2

i want to use rapidapi to get some data using redux, but when i console.log the data i will be getting GET https://coinranking1.p.rapidapi.com/coins/coins 401 (Unauthorized). please what am i doing wrong, someone help me out

here is my code on my store.js file

import { configureStore } from "@reduxjs/toolkit"
import {cryptoApi} from "../services/cryptoApi"

export default configureStore({
    reducer: {
         [cryptoApi.reducerPath]: cryptoApi.reducer,
    },
})

here is where i store the data am getting from rapidapi

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

const cryptoApiHeaders = {
    'X-RapidAPI-Host': 'coinranking1.p.rapidapi.com',
    'X-RapidAPI-Key': my-key
}

const baseUrl = 'https://coinranking1.p.rapidapi.com/coins'
const createRequest = (url) => ({url, Headers: cryptoApiHeaders })

export const cryptoApi = createApi({
    ReducerPath: "cryptoApi",
    baseQuery: fetchBaseQuery( { baseUrl } ),
    endpoints: (builder) => ({
        getCryptos: builder.query({
            query: () => createRequest("/exchanges")
        })
    })
})

export const { useGetCryptosQuery, } = cryptoApi;


import { useGetCryptosQuery } from "../services/cryptoApi"

    const {data, isFetching } = useGetCryptosQuery();
    console.log(data);
Emmanuel uzoezie
  • 433
  • 1
  • 5
  • 18

1 Answers1

0

Instead of this:

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

Try changing like this:

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

Also add coins or exchanges according to the data you need.

if you need data from exchanges add this:

query: () => createRequest("/exchanges")

or if you need coins data change like this:

query: () => createRequest("/coins")