How to use Axios with OpenAI API?

Wed Apr 19 2023

3 min read

Axios is a popular JavaScript library that provides an easy-to-use interface for making HTTP requests and handling responses in web applications and server-side environments.

In this guide, let’s learn how to call OpenAI API from Rapid API Hub using Axios.

Setting up Axios

Axios is available through npm, which is one of the package managers for Node.js. You can install Axios in your project by running the following command in your terminal:

sh
npm install axios

This command will download and install the latest version of Axios in your project. Once the installation is complete, you can import Axios into your JavaScript file using the require or import statement.

For example, if you are using require, you can add the following line at the top of your file:

js
const axios = require('axios');

If you are using import, you can add the following line at the top of your file:

js
import axios from 'axios';

Getting an API Key

To obtain an API key for OpenAI API, we can use Rapid API Hub. Rapid API Hub offers a centralized platform for developers to discover, test, and connect to various APIs, including OpenAI API. Go ahead and sign up if you haven’t already.

Loading component...

Once you are signed in, head to the Rapid API developer dashboard. Now select your app on the left side bar and select Security. Here you will find your Rapid API key.

Making GET requests with Axios

To make a GET request to OpenAI API using Axios, we can use the axios.get() method. This method sends an HTTP GET request to the specified API endpoint and returns a Promise that resolves to the response data.

Here's a sample GET request to OpenAI API from Rapid API using Axios:

js
const axios = require("axios");
axios.get('https://openai80.p.rapidapi.com/models/text-davinci-001', {
headers: {
'X-RapidAPI-Key': 'your-rapid-api-key',
'X-RapidAPI-Host': 'openai80.p.rapidapi.com'
}
})
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.error(error);
});

This GET request is requesting information about the "text-davinci-001" model from the OpenAI API.

Making POST requests with Axios

In this example, we will use the Axios library to make a POST request to the OpenAI API. The API endpoint we are targeting is /images/generations, which generates images based on a provided prompt.

js
const axios = require("axios");
const headers = {
'content-type': 'application/json',
'X-RapidAPI-Key': 'your-rapid-key',
'X-RapidAPI-Host': 'openai80.p.rapidapi.com'
};
const data = {
prompt: "A cute kitten",
n: 1,
size: "1024x1024"
};
axios.post("https://openai80.p.rapidapi.com/images/generations", data, { headers })
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.error(error);
});

This code sends a POST request to the OpenAI API's image generation endpoint to generate one image of a cute kitten with the size of 1024x1024. You can change the prompt, image size, and the number of images generated according to your preferences.

You will see something like this on your terminal:

Once you click on the URL, you will see the image of the prompt you provided. In this case, we would see a kitten in the size 1024x1024:

Wrap up

Axios provides a straightforward way to make HTTP requests to APIs like OpenAI. With just a few lines of code, you can easily retrieve or send data and handle responses in your applications.