Learn how to send user metadata with prompts to Gemini from n8n by setting up an HTTP request node, configuring authentication, dynamic payloads, error handling, and security best practices.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
To send user metadata along with prompts to Gemini from n8n, you'll need to set up an HTTP request node that includes the user metadata in the API call payload. You'll need to format your API request with both the prompt text and the user metadata object in the JSON body, authenticate with your API key, and configure the proper endpoints for the Gemini API.
Step 1: Set Up a New Workflow in n8n
First, let's create a new workflow in n8n:
Step 2: Add a Trigger Node
You need a trigger to start your workflow:
Step 3: Add HTTP Request Node
Now, let's add an HTTP Request node to communicate with the Gemini API:
Step 4: Configure API Key Authentication
To authenticate with the Gemini API:
For better security, consider using n8n credentials or environment variables to store your API key rather than hardcoding it.
Step 5: Prepare the Payload with User Metadata
Now, let's structure the request body to include both the prompt and user metadata:
{
"contents": [
{
"parts": [
{
"text": "Your prompt text here"
}
]
}
],
"generationConfig": {
"temperature": 0.7,
"maxOutputTokens": 1024
},
"safetySettings": [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_MEDIUM_AND\_ABOVE"
}
],
"userMetadata": {
"userId": "user-123",
"userLocation": "New York",
"userLanguage": "en-US",
"customData": {
"subscription": "premium",
"lastLogin": "2023-06-15T14:30:00Z"
}
}
}
Step 6: Make the Request Dynamic
Let's modify the request to use dynamic data:
{
"contents": [
{
"parts": [
{
"text": "{{$json.promptText}}"
}
]
}
],
"generationConfig": {
"temperature": {{$json.temperature || 0.7}},
"maxOutputTokens": {{$json.maxTokens || 1024}}
},
"safetySettings": [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_MEDIUM_AND\_ABOVE"
}
],
"userMetadata": {
"userId": "{{$json.userId}}",
"userLocation": "{{$json.userLocation}}",
"userLanguage": "{{$json.userLanguage}}",
"customData": {{$json.customData || "{}"}}
}
}
Step 7: Add a Set Node to Provide Input Data
To feed data into the HTTP Request node:
{
"promptText": "Write a short story about an adventure in space",
"temperature": 0.8,
"maxTokens": 2048,
"userId": "user-456",
"userLocation": "Berlin",
"userLanguage": "de-DE",
"customData": {
"subscription": "premium",
"userPreferences": {
"genre": "sci-fi",
"complexity": "advanced"
}
}
}
Step 8: Add Error Handling
Let's add error handling to the workflow:
{{$json.statusCode}} >= 400
Step 9: Process the Gemini Response
To extract and process the Gemini API response:
{
"generatedText": "{{$json.body.candidates[0].content.parts[0].text}}",
"safetyRatings": "{{$json.body.candidates[0].safetyRatings}}",
"finishReason": "{{$json.body.candidates[0].finishReason}}",
"rawResponse": "{{$json.body}}"
}
Step 10: Test the Workflow
Now, let's test our workflow:
Step 11: Create a Reusable Workflow
To make this workflow reusable:
Step 12: Set Up Proper Error Logging
Enhance your error handling:
// Format error message
const errorDetails = {
statusCode: $input.item.json.statusCode,
statusMessage: $input.item.json.statusMessage,
errorMessage: $input.item.json.body?.error?.message || "Unknown error",
timestamp: new Date().toISOString()
};
// Return formatted error
return {
error: errorDetails,
originalInput: $input.item.json.options?.body || {}
};
Step 13: Implement Retry Logic
Add retry capability for API call failures:
Step 14: Add Support for Different Gemini Models
Make your workflow flexible for different Gemini models:
{
"model": "gemini-pro",
"promptText": "Write a short story about an adventure in space",
// other parameters as before
}
https://generativelanguage.googleapis.com/v1beta/models/{{$json.model}}:generateContent
Step 15: Implement Security Best Practices
Enhance security for your workflow:
Final Notes
When working with the Gemini API and user metadata:
By following this tutorial, you've created a workflow in n8n that can send user metadata along with prompts to the Gemini API, allowing for more personalized and context-aware AI responses.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.