I'm very new to ReTool and hoping this is an obvious thing I've just not found the answer to. I have a REST API resource (callMyREST
) which works perfectly fine when I use it with events, etc. But I'd like to call it from some JS code. Like callMyREST("bar")
and have that REST API resource use that value in one of its URL parameters like
https://apiurl.com/?foo=bar
But for some reason, I'm struggling to find how I pass the variable and define it within the resource. Is there a how-to guide I'm overlooking somewhere? Thanks for any guidance here!
1 Like
@acafourek Welcome to the community! ![:wave: :wave:](https://community.retool.com/images/emoji/apple/wave.png?v=12)
Here is couple of steps which should help.
- Create the REST API Resource:
- Go to the "Resources" section in Retool and create a new REST API resource.
- Set up the resource with the base URL. For example,
https://apiurl.com
.
- Define a Query for the REST API Resource:
- Go to the "Queries" tab and create a new query.
- Select the REST API resource you created.
- Set up the query to use a dynamic URL parameter. For example:
{{ baseUrl }}/?foo={{ urlParam }}
- Ensure you define
baseUrl
and urlParam
as variables in the query.
- Call the Query from JavaScript:
- You can use JavaScript to call the query and pass parameters dynamically using the
trigger
function.
Example js code
// Define the parameter value
const paramValue = "bar";
// Call the query and pass the parameter
callMyREST.trigger({
additionalScope: {
urlParam: paramValue
},
onSuccess: (data) => {
console.log("Query succeeded:", data);
},
onFailure: (error) => {
console.log("Query failed:", error);
}
});
additionalScope
is used to pass dynamic values to the query.
urlParam
in additionalScope
corresponds to the {{ urlParam }}
variable used in the query URL.
onSuccess
and onFailure
are optional callbacks to handle the success or failure of the query.
1 Like
Ah thank you so much; I was very close but wasn't using the additionalScope
quite correctly and this clarified for me how to think about things.