Help

POST JSON request with scripting app

Topic Labels: Automations
3091 3
cancel
Showing results for 
Search instead for 
Did you mean: 
The_More
4 - Data Explorer
4 - Data Explorer

Hi there,

I would like to post a json request to an api with the locations on my base. The json structure looks like this:

{
  "stops": [
    {
      "id": "order-1-dropoff",
      "position": {
        "lon": -96.7524,
        "lat": 32.65764
      },
      "quantity": -15
    },
    {
      "id": "order-2-dropoff",
      "position": {
        "lon": -96.82663,
        "lat": 32.6498
      },
      "quantity": -10
    },
    {
      "id": "order-3-dropoff",
      "position": {
        "lon": -96.9925,
        "lat": 32.8922
      },
      "quantity": -15
    },
    {
      "id": "order-4-dropoff",
      "position": {
        "lon": -96.99433,
        "lat": 32.89166
      },
      "quantity": -5
    },
    {
      "id": "order-5-dropoff",
      "position": {
        "lon": -96.88486,
        "lat": 32.81559
      },
      "quantity": -20
    },
    {
      "id": "order-6-dropoff",
      "position": {
        "lon": -96.85313,
        "lat": 32.80997
      },
      "quantity": -20
    },
    {
      "id": "order-7-dropoff",
      "position": {
        "lon": -96.58497,
        "lat": 32.73941
      },
      "quantity": -20
    },
    {
      "id": "order-8-dropoff",
      "position": {
        "lon": -96.6335,
        "lat": 32.7274
      },
      "quantity": -20
    },
    {
      "id": "order-9-dropoff",
      "position": {
        "lon": -96.67937,
        "lat": 32.77418
      },
      "quantity": -20
    },
    {
      "id": "order-10-dropoff",
      "position": {
        "lon": -96.69089,
        "lat": 32.77412
      },
      "quantity": -10
    }
  ]
}

I would like to change the "stops" with the locations on my database. How can I do this? Or can I?

Thank you!

3 Replies 3

Welcome to the Airtable community!

It probably can be done. (Scripting can interact with most APIs, but not all.)

Whether or not you can do this depends on your level of scripting/coding experience and your interest in learning. We would need to know more about your coding experience to provide more specific advice.

Thank you!

I have worked with simple api requests before however they were only for sending one record from my base to the api. In this case I need to send all of my records in one single request. I have used something like this before:


let body = {
        "name": "Tenali Ramakrishna",
        "gender": "male",
        "email": "tenali.ramakrishna2@15ce.com",
        "status": "active"
    }
let response = await fetch('https://example.com', {
    method: 'POST',
    body: JSON.stringify(body),
    headers: {
        "Content-Type": "application/json",
        "Accept": "application/json",
        'Authorization': 'Bearer key',
    }, 
});
console.log(await response.json());

I have found something like this on airtable community, maybe this can help if we change the destination to my api endpoint?

// Call out the tables we will use
let table = base.getTable('TABLE_NAME');
let query = await table.selectRecordsAsync();
let destination = base.getTable('Scoring');

// pull combined info for assigned project/judge pairs
query.records.forEach(project => {
            let Judges = project.getCellValue('Judges');
            if (Judges) {
                Judges.forEach(judge => {
                   let recordId = destination.createRecordAsync(
                                {
                                    'Name': `${project.name} evaluated by ${judge.name}`,
                                    'Judge': [{id: judge.id}],
                                    'Projects': [{id: project.id}],
                                },
                        );
                    console.log("Success!")
                });
            };
         });

The example code you posted has all hardcoded values, and not values from a record, but you said that you were able to send data from a record, so I assume that you can already get values from a single record.

Sounds like the main thing you need to figure out is how to create a request body with data from multiple records. This requires getting all the records and creating an array of record data. Have you worked with arrays before? How about selecting all the records in a table?

This code sample creates Airtable records one at a time. It does not call an api, and it does not work in batches. Other than the first two lines, it is not a good starting place for your task.