I’m pretty new to n8n and programming overall, so this is probably a basic question. I can’t figure out how to link a Function node to an HTTP Request node when it’s inside an AI Agent workflow.
What I’m trying to do is make an HTTP call to the Google Places API to find new cafes around my location. After getting the data back, I want to use a Function node to filter everything so I only see places with ratings above 4.5 and limit it to just the first 8 results. Everything I’ve found online says to connect the HTTP Request node directly to a Function node, but I’m having trouble making this work in my current setup. Has anyone dealt with this before?
Had the same issue when I built a restaurant data workflow. What got me was making sure the HTTP Request node actually worked before the Function node tried to process anything. Google Places API sometimes returns errors or empty results, and your Function node will crash if you don’t handle that. Add some error checking at the top of your Function node - check if the response exists and has results before you start filtering. Also, double-check your API key permissions. I wasted hours thinking it was a connection problem when my API config was just wrong. AI Agent workflows run differently than regular ones, so test each node separately first.
You’re unable to connect an n8n Function node to an HTTP Request node within an AI Agent workflow to filter results from the Google Places API. You want to filter for places with ratings above 4.5 and limit the results to the top 8.
Understanding the “Why” (The Root Cause):
The core issue is likely a combination of how the Google Places API returns data and how n8n’s Function node expects its input and requires formatted output. Additionally, error handling within the Function node is crucial, as the API might return errors or empty results. AI Agent workflows, while appearing different, operate on the same fundamental principles of node connections. The order of execution matters: the HTTP Request node must successfully complete before the Function node can process its output. Finally, ensure your Google Places API key has the necessary permissions and is correctly configured.
Step-by-Step Guide:
Verify HTTP Request Node Functionality: Before connecting the Function node, thoroughly test your HTTP Request node to ensure it successfully retrieves data from the Google Places API. Inspect the node’s execution results to confirm it returns a valid JSON response with the expected data structure (including the results array). Pay close attention to error codes and messages.
Implement Error Handling and Data Validation in the Function Node: Add error handling to your Function node to gracefully handle potential issues such as empty responses from the API. Check for the existence of the response and the results array within the response before attempting to process data. Consider a structure like this:
if (!$input.all()[0] || !$input.all()[0].json || !$input.all()[0].json.results) {
return []; // Return an empty array if there's an error or no results
}
const results = $input.all()[0].json.results;
const filteredResults = results.filter(place => place.rating > 4.5).slice(0, 8);
return filteredResults.map(place => ({json: place}));
Correct Data Transformation within the Function Node: The code provided in the original answer correctly filters and slices the array, but it’s crucial to wrap each result in a json object ( {json: place} ) to adhere to n8n’s data format. Ensure that your Function node’s input ($input.all()[0].json.results) accurately reflects the path to the results array in the API’s JSON response.
Review Google Places API Configuration: Double-check that your Google Places API key is correctly configured in the HTTP Request node and that the key has sufficient permissions to access the necessary data.
Common Pitfalls & What to Check Next:
Incorrect API Key: Verify your Google Places API key is valid and has the necessary permissions.
Rate Limits: Google Places API has rate limits. If you’re exceeding them, you might receive errors. Check the API documentation for details.
JSON Response Structure: Carefully inspect the JSON response from the Google Places API to confirm the exact structure, including the path to the results array and the properties of each place (e.g., rating). Adapt your Function node code accordingly.
Network Connectivity: Ensure a stable network connection.
n8n Workflow Configuration: Double-check that you’ve correctly connected the HTTP Request node to the Function node, and that the workflow is configured to run in the right order.
Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!