Add rows to a table
One script reply has been approved by the moderators Verified

Deprecated in favor of records endpoints. We have no immediate plans to remove these endpoints, but consider records a better starting point for new projects.

Created by hugo697 329 days ago
Submitted by hugo697 Bun
Verified 329 days ago
1
//native
2
type Grist = {
3
  apiKey: string;
4
  host: string;
5
};
6
/**
7
 * Add rows to a table
8
 * Deprecated in favor of `records` endpoints. We have no immediate plans to remove these endpoints, but consider `records` a better starting point for new projects.
9
 */
10
export async function main(
11
  auth: Grist,
12
  docId: string,
13
  tableId: string,
14
  noparse: string | undefined,
15
  body: {},
16
) {
17
  const url = new URL(
18
    `https://${auth.host}/api/docs/${docId}/tables/${tableId}/data`,
19
  );
20
  for (const [k, v] of [["noparse", noparse]]) {
21
    if (v !== undefined && v !== "" && k !== undefined) {
22
      url.searchParams.append(k, v);
23
    }
24
  }
25
  const response = await fetch(url, {
26
    method: "POST",
27
    headers: {
28
      "Content-Type": "application/json",
29
      Authorization: "Bearer " + auth.apiKey,
30
    },
31
    body: JSON.stringify(body),
32
  });
33
  if (!response.ok) {
34
    const text = await response.text();
35
    throw new Error(`${response.status} ${text}`);
36
  }
37
  return await response.json();
38
}
39