How exactly do you authenticate you api key?

When searching for a coin (say BTC) how exactly do you authenticate you api key, like what would be the exact link you put in the browser. Im new with APIs and having a hard time with the intro part of the docs and authenticating your api key.

2 Likes

Hi @HUH_Clipz, that’s a good question. Authenticating by putting the API key into your URL is not very secure. What you could do is add the API key to your header when doing a request.

Read more about authentication over here: Coinranking API Documentation - Introduction for Developers

Hi @mark , the api key works perfectly fine through curl but when I make a get request through axios, it gives error.

This is my exact piece of code:

const axios = require("axios");
require("dotenv").config();

const fetchPairs = async () => {
  try {
    const response = await axios.get("https://api.coinranking.com/v2/coins", {
      headers: { "x-access-token": process.env.COINRANKING_API_KEY },
    });

    console.log(response.data);

    // If you want to log headers
    console.log(response.headers);
  } catch (error) {
    console.log(error.response ? error.response.data : error.message);
  }
};

fetchPairs();

And this is the response:

{
status: ‘fail’,
code: ‘UNAUTHORIZED’,
message: ‘Invalid API key, see the authentication section of the documentation to see how to authorize correctly: Coinranking API Documentation - Introduction for Developers’
}

Can you help me how to authenticate it correctly through javascript because it worked fine previously.

P.S the API key is perfectly correct.

Hi @Ali_Akbar, thanks for reaching out.

The code looks solid. Perhaps your env file isn’t working correctly. You could try to put the key into the code directly. Make it hardcoded and see if it works in that way.

Hi, :wave:

As per my knowledge, to authenticate your API key when searching for a coin like BTC, you usually need to include the key in the HTTP headers of your request rather than using a link in the browser. For example, with many APIs, you would send a GET request to a specific endpoint and include your API key in the headers.

curl -H “Authorization: Bearer YOUR_API_KEY” “https://api.example.com/v1/coins/BTC”

Replace YOUR_API_KEY with your actual API key and https://api.example.com/v1/coins/BTC with the correct endpoint for your API.

Check the API documentation for the exact details, as the method can vary between services. I hope this will help you!

I hope this will help you!

Respected community member! :smiling_face_with_three_hearts:

1 Like

Thank you @chitramishra for your thorough answer, I appreciate your help. :smiley:

1 Like

It’s my pleasure! :blush:
I am really happy my answer is helpful for you!

1 Like