0

Create sign-in token

by
Published Apr 8, 2025

Creates a new sign-in token and associates it with the given user. By default, sign-in tokens expire in 30 days. You can optionally supply a different duration in seconds using the `expires_in_seconds` property.

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 sign-in token
7
 * Creates a new sign-in token and associates it with the given user.
8
By default, sign-in tokens expire in 30 days.
9
You can optionally supply a different duration in seconds using the `expires_in_seconds` property.
10
 */
11
export async function main(
12
  auth: Clerk,
13
  body: { user_id?: string; expires_in_seconds?: number },
14
) {
15
  const url = new URL(`https://api.clerk.com/v1/sign_in_tokens`);
16

17
  const response = await fetch(url, {
18
    method: "POST",
19
    headers: {
20
      "Content-Type": "application/json",
21
      Authorization: "Bearer " + auth.apiKey,
22
    },
23
    body: JSON.stringify(body),
24
  });
25
  if (!response.ok) {
26
    const text = await response.text();
27
    throw new Error(`${response.status} ${text}`);
28
  }
29
  return await response.json();
30
}
31