1

Create Multiple Records

by
Published Jun 6, 2022

Create any number of records in a table, sent in batches of 10.

Script airtable Verified

The script

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

3
export type DynSelect_base_id = string
4
export type DynSelect_table_id = string
5

6
// Dropdown of the bases the token can access.
7
export async function base_id(auth: RT.Airtable) {
8
  const options: { value: string; label: string }[] = []
9
  let offset: string | undefined = undefined
10
  do {
11
    const url = new URL("https://api.airtable.com/v0/meta/bases")
12
    if (offset) {
13
      url.searchParams.append("offset", offset)
14
    }
15
    const response = await fetch(url, {
16
      headers: {
17
        Authorization: `Bearer ${auth.apiKey}`,
18
        Accept: "application/json",
19
      },
20
    })
21
    if (!response.ok) {
22
      throw new Error(`${response.status} ${await response.text()}`)
23
    }
24
    const page = (await response.json()) as {
25
      bases: { id: string; name: string }[]
26
      offset?: string
27
    }
28
    options.push(...page.bases.map((b) => ({ value: b.id, label: b.name })))
29
    offset = page.offset
30
  } while (offset)
31
  return options
32
}
33

34
async function listTables(auth: RT.Airtable, base_id: string) {
35
  if (!base_id) {
36
    return []
37
  }
38
  const response = await fetch(
39
    `https://api.airtable.com/v0/meta/bases/${base_id}/tables`,
40
    {
41
      headers: {
42
        Authorization: `Bearer ${auth.apiKey}`,
43
        Accept: "application/json",
44
      },
45
    }
46
  )
47
  if (!response.ok) {
48
    throw new Error(`${response.status} ${await response.text()}`)
49
  }
50
  const { tables } = (await response.json()) as {
51
    tables: {
52
      id: string
53
      name: string
54
      fields: { id: string; name: string; type: string }[]
55
      views: { id: string; name: string }[]
56
    }[]
57
  }
58
  return tables
59
}
60

61
// Dropdown of the tables in the chosen base.
62
export async function table_id(auth: RT.Airtable, base_id: DynSelect_base_id) {
63
  return (await listTables(auth, base_id)).map((t) => ({
64
    value: t.id,
65
    label: t.name,
66
  }))
67
}
68

69
/**
70
 * Create Multiple Records
71
 * Create any number of records in a table. Each item of records is an object keyed by field name. Sent in batches of 10 (the API maximum), so a failure part-way leaves the earlier batches created.
72
 */
73
export async function main(
74
  auth: RT.Airtable,
75
  base_id: DynSelect_base_id,
76
  table_id: DynSelect_table_id,
77
  records: { [key: string]: any }[],
78
  typecast: boolean | undefined,
79
  return_fields_by_field_id: boolean | undefined
80
) {
81
  const created: any[] = []
82
  // The API accepts at most 10 records per request.
83
  for (let i = 0; i < records.length; i += 10) {
84
    const response = await fetch(
85
      `https://api.airtable.com/v0/${base_id}/${table_id}`,
86
      {
87
        method: "POST",
88
        headers: {
89
          Authorization: `Bearer ${auth.apiKey}`,
90
          "Content-Type": "application/json",
91
          Accept: "application/json",
92
        },
93
        body: JSON.stringify({
94
          records: records.slice(i, i + 10).map((fields) => ({ fields })),
95
          typecast: typecast === true,
96
          returnFieldsByFieldId: return_fields_by_field_id === true,
97
        }),
98
      }
99
    )
100
    if (!response.ok) {
101
      throw new Error(`${response.status} ${await response.text()}`)
102
    }
103
    created.push(...(await response.json()).records)
104
  }
105

106
  return { records: created }
107
}
108

Other submissions
  • Submitted by rossmccrann Deno
    Created 1162 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 529 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
    }