Generate API call snippets using RapidAPI Client for VS Code

Mon Jul 25 2022

5 min read

Regardless of the framework or library, we need to write APIs to build the client-server architecture. Similarly, we also have to consume APIs for various reasons, mainly to perform CRUD operations using a REST API. The REST API can be public or private. If you are looking for a public REST API, I recommend looking at RapidAPI Hub, which is home to 35000+ APIs.

RapidAPI has recently launched a VS Code extension called RapidAPI Client for VS Code that provides you with all the required capabilities to test an API. The extension provides a graphical user interface where you insert all the necessary values to make the API call. You can also generate a code snippet in several languages using it.

Before we go any further, let’s quickly look at how you can download the extension.

Installation

It is straightforward to install RapidAPI Client for VS Code. Just in case there is any confusion, the following are steps you can follow to install it.

→ STEP #1

Click on the Extension's icon on the VS Code sidebar to open the Extension’s marketplace. You can also press CMD + SHIFT + X to open it if you are on Mac. For Windows, it would be CTRL + SHIFT + X.

Loading component...

→ STEP #2

Now click on the install button. It will take a moment; afterward, you will see the RapidAPI icon in your VS Code sidebar.

Generating Code Snippets

Let’s look at how you can generate code snippets in different languages using the RapidAPI Client extension. But before I go there, we need to find an API to test.

Finding An API

We will use the Famous Quotes API. For this, head over to the RapidAPI Hub and create an account. Then search for Famous Quotes API in the search box.

Select the Famous Quotes API from the search results.

To use this API, you need to subscribe to it first. You can do this by clicking on Subscribe to Test button.

Once you click the button, you will be redirected to another page where different available subscription packages will be shown. Let’s go with the free one for now.

After all this, you will be redirected back to the original page. Here you will have a key x-rapidapi-key. Go ahead and save it somewhere. We will have to send it in the API request header while testing.

Creating a Request

Now go ahead and create a new request inside the RapidAPI Client. Fill out all the fields, including the HTTP request method, API endpoint URL, query parameters, header values, etc. You can look at this guide to learn more about how you can fill out these fields and test an API.

Generating Python Code Snippets

After inserting values in all the appropriate fields, go ahead and select Python from the dropdown menu in front of the Request Snippet at the bottom. It will show another dropdown menu where you can select the python package you want to use to generate the code snippet. Let’s keep it http.client for now.

Now click on the </> icon. The extension will create a new python file and paste the code snippet there. The following is what the extension has generated:

py
import http.client
conn = http.client.HTTPSConnection("famous-quotes4.p.rapidapi.com")
headers = {
'X-RapidAPI-Host': "famous-quotes4.p.rapidapi.com",
'X-RapidAPI-Key': "YOUR-RAPIDAPI-KEY"
}
conn.request("GET", "/random?category=all&count=5", headers=headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

From the second dropdown menu, select Requests instead of http.client. Now click on the </> icon again. You will see another python file created with the code snippet inside it.

py
import requests
url = "https://famous-quotes4.p.rapidapi.com/random"
querystring = {"category":"all","count":"5"}
headers = {
"X-RapidAPI-Host": "famous-quotes4.p.rapidapi.com",
"X-RapidAPI-Key": "YOUR-RAPIDAPI-KEY"
}
response = requests.request("GET", url, headers=headers, params=querystring)
print(response.text)

Generating Go Code Snippet

Let’s quickly generate a Go code snippet to call APIs using the RapidAPI Client. For this, select Go (NewRequest) from the dropdown menu in front of the Request snippet at the bottom.

Now click on the </> icon to generate the snippet.

go
package main
import (
"fmt"
"net/http"
"io/ioutil"
)
func main() {
url := "https://famous-quotes4.p.rapidapi.com/random?category=all&count=5"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("X-RapidAPI-Host", "famous-quotes4.p.rapidapi.com")
req.Header.Add("X-RapidAPI-Key", "YOUR-RAPIDAPI-KEY")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(res)
fmt.Println(string(body))
}

Generating JavaScript Code Snippet

The RapidAPI Client for VS Code extension lets you generate different JavaScript code snippets. You can select the type of code snippet you want to generate. For JavaScript, you can choose jQuery, fetch, XMLHttpRequest, and Axios.

Let’s select JavaScript and then fetch from the drop-down menu.

Now click on the </> icon to generate the snippet.

js
const options = {
method: 'GET',
headers: {
'X-RapidAPI-Host': 'famous-quotes4.p.rapidapi.com',
'X-RapidAPI-Key': 'YOUR-RAPIDAPI-Key'
}
};
fetch('https://famous-quotes4.p.rapidapi.com/random?category=all&count=5', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
Loading component...

Wrap Up

You can see how easy it is to generate API code snippets in different languages using the RapidAPI Client VS Code extension. Now go ahead and try it out yourself. We have also written a detailed introduction about RapidAPI Client for VS Code that you can find here.

If you want to learn how to generate interfaces for your API response, we have written a separate piece about it that you can find here.