I am working on a Chainlink off-chain function where I need to fetch patient data from an external API. The API requires an API key for authentication, which I have stored in secrets.apiKey. However, when I include the API key in the request headers, the response object indicates an error. When I remove the authentication requirement from the API and omit the API key, the function works correctly and retrieves the expected data.
Here is the code snippet :
if (!secrets.apiKey) {
throw Error("api key not set");
}
// Make HTTP request to fetch patient data from external API
const apiUrlPatientData = 'https://ipfs-api-production-4146.up.railway.app/getTestData'; // Replace with actual API URL
const patientDataRequest = Functions.makeHttpRequest({
url: apiUrlPatientData,
headers: {
"Content-Type": "application/json",
"x-api-key": secrets.apiKey,
},
});
const response = await patientDataRequest;
if (response.error) {
console.log("error");
} else {
console.log(response.data.data);
}
return Functions.encodeUint256(response.data.data[0]);
Problem:
->When I include the x-api-key in the headers, the response object has an error and does not fetch the data.
->Without the x-api-key header (and with the API not requiring authentication), the function works and retrieves the data correctly.
Questions:
->Why does adding the x-api-key header cause an error in fetching the response?
->How can I properly include the API key in the request headers to authenticate and fetch the data successfully?