0

Create Trigger

by
Published Dec 20, 2024

Creates a new trigger for a given function in a namespace. To create a trigger, send a POST request to `/v2/functions/namespaces/$NAMESPACE_ID/triggers` with the `name`, `function`, `type`, `is_enabled` and `scheduled_details` properties.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Create Trigger
7
 * Creates a new trigger for a given function in a namespace. To create a trigger, send a POST request to `/v2/functions/namespaces/$NAMESPACE_ID/triggers` with the `name`, `function`, `type`, `is_enabled` and `scheduled_details` properties.
8
 */
9
export async function main(
10
  auth: Digitalocean,
11
  namespace_id: string,
12
  body: {
13
    name: string;
14
    function: string;
15
    type: string;
16
    is_enabled: false | true;
17
    scheduled_details: { cron: string; body?: { name?: string } };
18
  },
19
) {
20
  const url = new URL(
21
    `https://api.digitalocean.com/v2/functions/namespaces/${namespace_id}/triggers`,
22
  );
23

24
  const response = await fetch(url, {
25
    method: "POST",
26
    headers: {
27
      "Content-Type": "application/json",
28
      Authorization: "Bearer " + auth.token,
29
    },
30
    body: JSON.stringify(body),
31
  });
32
  if (!response.ok) {
33
    const text = await response.text();
34
    throw new Error(`${response.status} ${text}`);
35
  }
36
  return await response.json();
37
}
38