Get Access Token
One script reply has been approved by the moderators Verified

These are the routes for authing the API and going through the OAuth flow.

Applications utilizing a personal API token don't use this endpoint.

Note: OAuth tokens are not supported when using the Try It feature of our Reference docs. You can't try this endpoint from your web browser.

Created by hugo697 168 days ago Picked 2 times
Submitted by hugo697 Bun
Verified 168 days ago
1
//native
2
type Clickup = {
3
  token: string;
4
};
5
/**
6
 * Get Access Token
7
 * These are the routes for authing the API and going through the [OAuth flow](doc:authentication).\
8
 \
9
Applications utilizing a personal API token don't use this endpoint.\
10
 \
11
***Note:** OAuth tokens are not supported when using the [**Try It** feature](doc:trytheapi) of our Reference docs. You can't try this endpoint from your web browser.*
12

13
 */
14
export async function main(
15
  auth: Clickup,
16
  client_id: string | undefined,
17
  client_secret: string | undefined,
18
  code: string | undefined,
19
) {
20
  const url = new URL(`https://api.clickup.com/api/v2/oauth/token`);
21
  for (const [k, v] of [
22
    ["client_id", client_id],
23
    ["client_secret", client_secret],
24
    ["code", code],
25
  ]) {
26
    if (v !== undefined && v !== "" && k !== undefined) {
27
      url.searchParams.append(k, v);
28
    }
29
  }
30
  const response = await fetch(url, {
31
    method: "POST",
32
    headers: {
33
      Authorization: auth.token,
34
    },
35
    body: undefined,
36
  });
37
  if (!response.ok) {
38
    const text = await response.text();
39
    throw new Error(`${response.status} ${text}`);
40
  }
41
  return await response.json();
42
}
43