Code Tool Example with a RapidAPI Python code

Hi, i´m just testing the CODE TOOL ( LANGCHAIN) and wonder if you have any example of how to use it with a GET REQUEST for example. I had tested with Python with no success. ( ERROR UNDEFINDED)
And also how to Pass the Payload with-in the CODE. thanks!

here es the code use it:
import http.client

conn = http.client.HTTPSConnection(“social-links-search.p.rapidapi.com”)

headers = {
‘X-RapidAPI-Key’: “XXXXXXXXXXXXXXXXX”,
‘X-RapidAPI-Host’: “social-links-search.p.rapidapi.com
}

conn.request(“GET”, “/search-social-links?query=John%20Smith&social_networks=facebook%2Ctiktok%2Cinstagram%2Csnapchat%2Ctwitter%2Cyoutube%2Clinkedin%2Cgithub%2Cpinterest”, headers=headers)

res = conn.getresponse()
data = res.read()

print(data.decode(“utf-8”))

Hi @Juan_Merodio, welcome to the community!

The Python support in n8n is based on Pyodide, which means you can only use some packages: Code node | n8n Docs

http.client is not among these, the respective issue is still open: https://github.com/pyodide/pyodide/issues/140

So perhaps you want to consider using JS code here? You can use this.helpers.httpRequest to make HTTP Requests as part of n8n’s code snippets, our documentation and this forum have a few examples (mostly for the original Code node and custom nodes, but they should work regardless of where they are used).

1 Like

Thanks a lot! :wink: by the way will be great if you can point me to the JS code as i´m not sure what i have to look for. thanks again! :wink:

Hey,
You can try something like this. I can’t test it without valid credentials, though.

const headers = {
    'X-RapidAPI-Key': 'XXXXXXXXXXXXXXXXX',
    'X-RapidAPI-Host': 'social-links-search.p.rapidapi.com'
};

const options = {
    uri: 'https://social-links-search.p.rapidapi.com/search-social-links',
    qs: {
        query: 'John Smith',
        social_networks: 'facebook,tiktok,instagram,snapchat,twitter,youtube,linkedin,github,pinterest'
    },
    headers: headers,
    method: 'GET',
    json: true
};

return this.helpers.httpRequest(options)
    .then(response => {
        // Assuming response is an array of objects
        return response.map(item => {
            return {
                json: item
            };
        });
    })
    .catch(error => {
        console.error(error);
        // Return an empty array on error
        return [];
    });
1 Like