Chainlink Functions: Spotify API returns 400 Bad Request (x-www-form-urlencoded issue in Deno sandbox)

4 hours ago 2
ARTICLE AD BOX

I am developing a Chainlink Functions (v0.3.x) request to fetch artist data from the Spotify API. I am simulating the request locally using the Chainlink Functions Toolkit (simulateScript), which runs in a Deno sandbox.

I am able to load my secrets correctly, but the authentication request to Spotify consistently fails with 400 Bad Request. The Spotify API requires the body to be application/x-www-form-urlencoded, but Functions.makeHttpRequest seems to be failing to format this correctly regardless of the method I use.

Environment:

Framework: Hardhat

Library: @chainlink/functions-toolkit

Runtime: Deno (via Chainlink simulation)

API: https://accounts.spotify.com/api/token (Client Credentials Flow)

The Code (Current Attempt):
I am manually constructing the Base64 Authorization header and trying to pass the **grant_type**as a raw string.

// functions/spotify-functions.js const clientId = secrets.SPOTIFY_CLIENT_ID; const clientSecret = secrets.SPOTIFY_CLIENT_SECRET; // Manual Base64 encoding (Standard Node/Deno Buffer) const authString = `${clientId}:${clientSecret}`; const authBase64 = Buffer.from(authString).toString('base64'); const authRequest = Functions.makeHttpRequest({ url: "https://accounts.spotify.com/api/token", method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", "Authorization": `Basic ${authBase64}` }, // Trying to send raw string for form encoding data: "grant_type=client_credentials" }); const authResponse = await authRequest; if (authResponse.error) { // This logs: {"error":true,"message":"Bad Request","code":"400","response":{}} throw new Error(`Auth Failed: ${JSON.stringify(authResponse)}`); }

What I have tried:

Raw String Body: data: "grant_type=client_credentials" (Result: 400)

Object Body: data: { grant_type: "client_credentials" } (Result: 400 - likely serialized as JSON)

URL Query Params: url: ".../api/token?grant_type=client_credentials" with empty data (Result: 400)

Using npm:qs: Importing qs and using stringify({ grant_type: ... }) (Result: 400)

I have verified that my Client ID and Secret are correct (they work in a standard curl request).

How can I correctly send a POST request with application/x-www-form-urlencoded body using Functions.makeHttpRequest in the Chainlink Deno environment?

Read Entire Article