0

Create and send an organization invitation

by
Published Apr 8, 2025

Creates a new organization invitation and sends an email to the provided `email_address` with a link to accept the invitation and join the organization.

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 and send an organization invitation
7
 * Creates a new organization invitation and sends an email to the provided `email_address` with a link to accept the invitation and join the organization.
8
 */
9
export async function main(
10
  auth: Clerk,
11
  organization_id: string,
12
  body: {
13
    email_address: string;
14
    inviter_user_id?: string;
15
    role: string;
16
    public_metadata?: {};
17
    private_metadata?: {};
18
    redirect_url?: string;
19
  },
20
) {
21
  const url = new URL(
22
    `https://api.clerk.com/v1/organizations/${organization_id}/invitations`,
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