Create Multiple Records

Script airtable Verified

by nicolassirang ยท 6/6/2022

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 days ago
1
//native
2

3
type Airtable = {
4
  apiKey: string;
5
};
6

7
type AirtableTable = {
8
  baseId: string;
9
  tableName: string;
10
};
11
export async function main(
12
  atCon: Airtable,
13
  atTable: AirtableTable,
14
  recordList: object[],
15
) {
16
  const url = `https://api.airtable.com/v0/${atTable.baseId}/${encodeURIComponent(
17
    atTable.tableName,
18
  )}`;
19

20
  const response = await fetch(url, {
21
    method: "POST",
22
    headers: {
23
      Authorization: `Bearer ${atCon.apiKey}`,
24
      "Content-Type": "application/json",
25
    },
26
    body: JSON.stringify({
27
      records: recordList.map((fields) => ({ fields })),
28
    }),
29
  });
30

31
  if (!response.ok) {
32
    throw new Error(`${response.status} ${await response.text()}`);
33
  }
34

35
  const createMultiple = await response.json();
36

37
  return createMultiple;
38
}
39

Other submissions
  • Submitted by rossmccrann Deno
    Created 1027 days ago
    1
    import { Airtable } from "https://deno.land/x/airtable/mod.ts";
    2
    import { Field } from "https://deno.land/x/airtable/mod.ts";
    3
    
    
    4
    type Airtable = {
    5
      apiKey: string;
    6
    };
    7
    
    
    8
    type AirtableTable = {
    9
      baseId: string;
    10
      tableName: string;
    11
    };
    12
    export async function main(
    13
      at_con: Airtable,
    14
      at_table: AirtableTable,
    15
      new_record_list: object,
    16
      fields_list: object,
    17
    ) {
    18
      const airtable = new Airtable({ ...at_con, ...at_table });
    19
    
    
    20
      type Fields = fields_list;
    21
    
    
    22
      const createMultiple = await airtable.create<Fields>(new_record_list);
    23
    
    
    24
      return { message: "Created multiple records in table" };
    25
    }
    26
    
    
  • Submitted by hugo697 Deno
    Created 394 days ago
    1
    import { Airtable } from "https://deno.land/x/airtable/mod.ts";
    2
    
    
    3
    type Airtable = {
    4
      apiKey: string;
    5
    };
    6
    
    
    7
    type AirtableTable = {
    8
      baseId: string;
    9
      tableName: string;
    10
    };
    11
    export async function main(
    12
      atCon: Airtable,
    13
      atTable: AirtableTable,
    14
      recordList: object[],
    15
    ) {
    16
      const airtable = new Airtable({ ...atCon, ...atTable });
    17
    
    
    18
      const createMultiple = await airtable.create(recordList);
    19
    
    
    20
      return createMultiple;
    21
    }