0

Create an invitation

by
Published Apr 8, 2025

Creates a new invitation for the given email address and sends the invitation email. Keep in mind that you cannot create an invitation if there is already one for the given email address. Also, trying to create an invitation for an email address that already exists in your application will result to an error.

Script clerk Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Clerk = {
3
  apiKey: string;
4
};
5
/**
6
 * Create an invitation
7
 * Creates a new invitation for the given email address and sends the invitation email.
8
Keep in mind that you cannot create an invitation if there is already one for the given email address.
9
Also, trying to create an invitation for an email address that already exists in your application will result to an error.
10
 */
11
export async function main(
12
  auth: Clerk,
13
  body: {
14
    email_address: string;
15
    public_metadata?: {};
16
    redirect_url?: string;
17
    notify?: false | true;
18
    ignore_existing?: false | true;
19
    expires_in_days?: number;
20
    template_slug?: "invitation" | "waitlist_invitation";
21
  },
22
) {
23
  const url = new URL(`https://api.clerk.com/v1/invitations`);
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