{"id":21,"date":"2021-06-11T02:54:48","date_gmt":"2021-06-11T02:54:48","guid":{"rendered":"https:\/\/kevinyipeio.com\/blog\/?p=21"},"modified":"2021-06-11T03:10:23","modified_gmt":"2021-06-11T03:10:23","slug":"api-calls-with-axios-create","status":"publish","type":"post","link":"https:\/\/kevinyipeio.com\/blog\/2021\/06\/11\/api-calls-with-axios-create\/","title":{"rendered":"API calls with axios.create() (Almost like having an SDK for any API!)"},"content":{"rendered":"\n<p>Just a quick blog on something I discovered in the docs for axios.<\/p>\n\n\n\n<p>If you find yourself making many external API calls in your code via axios, you may find that you are repeating yourself with each API call, e.g. adding an Authorisation header each time you make a request, or for example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export async function sendStuffToMyAPI(id: number, data: SendData): Promise&lt;void&gt; {\n    const response: any = await axios.post(`${<strong>config.myAPIBaseUrl<\/strong>}\/entity\/${id}`, data, {\n        <strong>headers: {\n            Authorization: config.myToken,\n        },\n<\/strong>    });\n}\n\nexport async function getStuffFromMyAPI(id: number): Promise&lt;AxiosResponse&gt; {\n    const response: any = await axios.get(`${<strong>config.myAPIBaseUrl<\/strong>}\/entity\/${id}`, {\n       <strong> headers: {\n            Authorization: config.myToken,\n        },<\/strong>\n    });\n\n    return response.data;\n}\n<\/code><\/pre>\n\n\n\n<p>As seen above, we have to specify the headers and the Authorisation headers each time we make an API call. We can simplify this using axios.create(). To set up axios.create(), you give it some config, e.g. baseURL and headers and it returns an object that you can call the normal axios functions on, e.g. get, put, etc.<\/p>\n\n\n\n<p>As seen in the example below, it is kind of like having an SDK object that you can make calls to! First set up the axios object:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const <strong>myAPI<\/strong> = axios.create({\n    baseURL: <strong>config.myAPIBaseUrl<\/strong>,\n    headers:<strong> { Authorization: config.myToken }<\/strong>,\n});<\/code><\/pre>\n\n\n\n<p>Then you could simply make calls on the new myAPI object:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export async function sendStuffToMyAPI(id: number, data: SendData): Promise&lt;void> {\n    const response: AxiosResponse = await <strong>myAPI<\/strong>.post(`\/entity\/${id}`, data);\n}\n\nexport async function getStuffFromMyAPI(id: number): Promise&lt;AxiosResponse> {\n    const response: AxiosResponse = await <strong>myAPI<\/strong>.get(`\/entity\/${id}`);\n\n    return response.data;\n}<\/code><\/pre>\n\n\n\n<p>As you can see you can make api calls in your code a lot leaner with axios.create()! Note: I haven&#8217;t tested the above code so if there are syntax errors or other stuff please let me know!<\/p>\n\n\n\n<p>See here for more details: <a href=\"https:\/\/github.com\/axios\/axios#creating-an-instance\">https:\/\/github.com\/axios\/axios#creating-an-instance<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just a quick blog on something I discovered in the docs for axios. If you find yourself making many external<a class=\"more-link\" href=\"https:\/\/kevinyipeio.com\/blog\/2021\/06\/11\/api-calls-with-axios-create\/\"><span data-hover=\"Read More\">Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":32,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,9],"tags":[5,3,7,4,6],"_links":{"self":[{"href":"https:\/\/kevinyipeio.com\/blog\/wp-json\/wp\/v2\/posts\/21"}],"collection":[{"href":"https:\/\/kevinyipeio.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kevinyipeio.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kevinyipeio.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kevinyipeio.com\/blog\/wp-json\/wp\/v2\/comments?post=21"}],"version-history":[{"count":10,"href":"https:\/\/kevinyipeio.com\/blog\/wp-json\/wp\/v2\/posts\/21\/revisions"}],"predecessor-version":[{"id":51,"href":"https:\/\/kevinyipeio.com\/blog\/wp-json\/wp\/v2\/posts\/21\/revisions\/51"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kevinyipeio.com\/blog\/wp-json\/wp\/v2\/media\/32"}],"wp:attachment":[{"href":"https:\/\/kevinyipeio.com\/blog\/wp-json\/wp\/v2\/media?parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kevinyipeio.com\/blog\/wp-json\/wp\/v2\/categories?post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kevinyipeio.com\/blog\/wp-json\/wp\/v2\/tags?post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}