How to fetch the data in Airtable from API

In this lesson, You will learn How to fetch the data in Airtable from API. After this lesson, you can fetch data from any API. So, Let's get started. There are two ways to fetch data, let's learn step by step. First, Copy and paste the below code into your app. And try to run it, you will get data printed on your console from API.

Option 1

: Code :


let url = 'https://api.publicapis.org/entries';
let response = await remoteFetchAsync(url);
console.log(await response.json());

Understanding the code:

So, With just above 3 lines of code, you can fetch data from API. However, there is another way to fetch the data. We will discuss that in just a moment. Let's understand the above code first. In the above code, the first line is where you store the URL. Then in the second line of code, I am using the 'remoteFetchAsync' function and passing the URL as a parameter. So, the function fetches the data from that URL. Isn't it simple? Also, use 'await' in front of the function so that further code won't break. If you are with me from the beginning of the series, then you already know that. And finally, we are printing the results. I am using https://apipheny.io/free-api/ website/API for this demonstration purpose. But you can use any API to get the data. You can just replace the URL, extract the data and you are good to go.

Option 2

Now, Let's learn another way to fetch the data. You can replace the below code with the previous code.

: Code :


let url = 'https://api.publicapis.org/entries';
let response = await remoteFetchAsync(url, {method: 'GET',headers: {'Content-Type': 'application/json',},});
console.log(await response.json());

Understanding the code:

So, what is the difference between these two ways? Nothing much. The only difference is that we are using two different functions to fetch the data from the API. As you can see, everything else is the same as the previous one except the function. So let's discuss the function. In this function, I am passing two arguments or parameters.

  1. URL
  2. Options

URL is what you already know. In options, we are declaring what method we are using and what will be the content type. For this example, I am using the 'GET' method and expecting 'application/json' as a response. One important point here you need to understand is that Sometimes with the second way you might face CROS errors. However, with some improvements, you can fix it. So, that was how to fetch the data in airtable from API. In the next article, I will teach you how you can send data to API so stay connected for that. If you are facing any issues or need consultancy regarding Airtable, you can contact me by Clicking Here.

COPYRIGHT © 2022 · Monino Solutions ·