Share feedback, ideas and get community help

Updated 2 months ago

How to use a variable inside an OpenAI function tool?

At a glance
Greetings,

I have created an HTTP block that returns a JWT token and saves it to the token variable.
I also wrote an OpenAI assistant block that consumes a service from my API, which requires passing this token.

Here is the code I wrote in the Function handler of the OpenAI block:

try {
// I have read about the parsed/evaluated options, but neither seems to work...
// Tried const, let, var
const jwt = token;
const jwt = {{token}};

const apiUrl = "https://api.acme.com.br/v1/database/query";
const response = await fetch(apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": Bearer ${jwt}
},
body: JSON.stringify({ sql_query: sql_query })
});
const data = await response.json(); // Fixed JSON parsing here
return data;
} catch (error) {
throw error;
}

However, when I test it, I get the following error in the logs:

docker logs
2024-12-19 08:06:16 typebot-viewer | ReferenceError: token is not defined
2024-12-19 08:06:16 typebot-viewer | at eval (eval at <anonymous> (<isolated-vm>:3:10), <anonymous>:4:17)
2024-12-19 08:06:16 typebot-viewer | at <isolated-vm>:3:31
2024-12-19 08:06:16 typebot-viewer | at <isolated-vm>:4:3
2024-12-19 08:06:16 typebot-viewer | Error while executing script

Could anyone help me understand why the token variable is not recognized? Is there something wrong with how I am referencing it in the code?

Thanks in advance!
m
J
8 comments
Hi @José Eduardo I can help you
If you already have the value of the variable token why you are using a script js?
Trying to make it work
using it directly on "Authorization": Bearer ${token}, or {{token}} gives me an invalid reference too
Using "Authorization": Bearer ${{{token}}} i´ve got:
2024-12-19 08:55:35 typebot-viewer | ReferenceError: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9 is not defined
2024-12-19 08:55:35 typebot-viewer | at eval (eval at <anonymous> (<isolated-vm>:3:10), <anonymous>:9:36)
2024-12-19 08:55:35 typebot-viewer | at <isolated-vm>:3:31
2024-12-19 08:55:35 typebot-viewer | at <isolated-vm>:4:3
2024-12-19 08:55:35 typebot-viewer | Error while executing script
I don't think that using a script block to openai is a good option, why you don't use HTTP block?
it´s a callback function from openai to the server to get some data...
const JWT = '{{token}}';
const response = await fetch("https://api.acme.com.br/v1/database/query", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": Bearer ${JWT}
},
body: JSON.stringify({ "sql": sql_query })
});
const data = JSON.parse(response);
return data;

THIS WORKS, changed '{{token}}'
Add a reply
Sign up and join the conversation on Discord