reactjsreduxstorerapidapi

API Request to RAPID API gives StatusCode: 401,


import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';

const cryptoApiHeaders = {
  'x-rapidapi-key': 'coinranking1.p.rapidapi.com',
  'x-rapidapi-host': '935af67341msh072d3f07b71a1f8p1cb0b5jsn44961d90a001',
};

const baseUrl = 'https://coinranking1.p.rapidapi.com';

const createRequest = (url) => ({ url, headers: cryptoApiHeaders });

export const cryptoApi = createApi({
  reducerPath: 'cryptoApi',
  baseQuery: fetchBaseQuery({ baseUrl }),
  endpoints: (builder) => ({
    getCryptos: builder.query({
      query: () => createRequest('/coins'),
    }),
  }),
});

export const { useGetCryptosQuery } = cryptoApi;

** I tried to connect api from RapidApi but I keep getting 401 unauthorized. I couldn't figured out by myself :3 someone help me please **


Solution

  • Your headers should be this

    const cryptoApiHeaders = {
      'x-rapidapi-host': 'coinranking1.p.rapidapi.com',
      'x-rapidapi-key': '935af67341msh072d3f07b71a1f8p1cb0b5jsn44961d90a001',
    };