Run the script and return to your table and you should see something like this: Each of these attributes is mapped to the appropriate table field - 'ID': post.id. We then use the “dot” notation to access the attributes of the current post - post.id, erId and so on. As we iterate, each element of the data array is assigned to the post variable in turn. Note how the posts data from the fetch is mapped to our table. For each post we output some text to show which record is being created and then use Airtable’s createRecord method to create a new record. of iteration to loop through each record of the posts array we have already fetched. To start our script, we’re going to fetch data from JSONPlaceholder posts url and assign the response we get to a variable: We’ll see later in this series that there are other methods we can use with fetch to create, update, delete and more. The default method for fetch is GET - getting data from a resource. fetch allows us to make http calls to web servers, getting a document or data in return. We’re going to use JavaScript’s fetch methods for our script. Let’s add scripting app to out base and start coding. Our next step is to use an Airtable script to get the data and then insert it into our table. So, we’ve looked at the data that our API gives us and we have a table in our base to hold the data. We usually map by field name (API id maps to table ID, for example), so the order that the fields are presented to us by the API compared to the order of fields in our table doesn’t matter. When working with APIs field order is usually not relevant - we’re not mapping field one in our API to field one in our table. Notice that we have changed the order in our table to make the post id the primary field. Let’s create a table in Airtable with the same structure to hold the data. The body - the content body of the post.The id which is the unique identifier for the record.The userId - a non-unique value in the posts collection (a user might have many posts)."body": "quia et suscipit\nsuscipit recusandae consequuntur. "title": "sunt aut facere repellat provident occaecati. The structure of each object record in the array is pretty straightforward: You can usually request a single, specific record and this will be presented as a single object (Example here). Typically a REST API will present multiple instances of the requested resource as an array of objects (like posts above). The array is a series of objects (denoted by a pair of curly brackets containing a series of key/value pairs.).The data returned is an array (square brackets at the start and end).A couple of points to note at this stage: Scroll down the JSONPlaceholder homepage and you’ll see a list of common resources that it provides:Ĭlick on the /posts link and you’ll see a page of post data in JSON format. We’ll start by looking at what JSONPlaceholder can give us. We could connect to a specific app to do this, but we’re going to user JSONPlaceholder, which is a fake API resource, as our API provider, taking the place of the web app we want to integrate with. We’re going to start by getting some data from an external API and using it to create records in Airtable. This is part 1 of a series of posts that will explore how Airtable can interact with REST APIs to get, create, update and delete data in other applications. Most modern web apps have APIs and the most common design pattern for an API is REST (or RESTful). To get Airtable and another app to “talk” we’re going to need to use APIs. You might want to copy your projects to Asana, send an email using Sendgrid or get the latest stock prices. You don’t need to have anything else in your kitbag to make it work.īut sometimes we want Airtable to work with another app. A few tables and views, a couple of forms, maybe an automation or two and, of course, some scripts and you’re all set. For many Airtable use cases, Airtable is the only thing you need.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |