0

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?

1
  • whats the error?
    – ZeusLawyer
    Commented Jul 4 at 1:58

1 Answer 1

0

Hello Varad

There are some approaches related to authentication using Chainlink Functions.

Here are some tutorials to help you:

1
  • Hey Solange, I am using Function playground for running the above source code and I am using DON hosted secret in request. And I am getting above error on functions playground
    – Varad
    Commented May 25 at 4:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.