Problem using axios create method and how to use it

I’m trying to call public API to get IP address (https://api.ipify.org/) using axios (next app-typescript),

in my file index.ts :

import axios from "axios";
export const ipApi = axios.create({
    baseURL: "https://api.ipify.org",
})

and when i try to call it :

const getIP = await ipApi.get()

i got an error Expected 1-2 arguments, but got 0.ts(2554)
index.d.ts(223, 47): An argument for ‘url’ was not provided.

where did i go wrong ? is there something missing? Sorry for my bad english

>Solution :

Creating a axios instance with baseURL set just tell axios to prepend the URL given to the methods called using that instance. In your scenario you are calling the get() method without the required parameter url, and hence the error

The correct way of using the instance would be:

const getIP = await ipApi.get("/")

Which calls it on the root of the baseURL, essentially calling GET on https://api.ipify.org/

You can find the related docs here

Leave a Reply