0

Get Token Info

by
Published today

Return the account (hub ID), user and scopes behind the connected token.

Script hubspot Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 4 hours ago
1
//native
2

3
/**
4
 * Get Token Info
5
 * Return the account (hub ID), user and scopes behind the connected token. Works for private app / service key tokens and for OAuth access tokens.
6
 */
7
export async function main(auth: RT.Hubspot) {
8
  // Private app and service key tokens (pat-...) use their own introspection endpoint.
9
  const response = auth.token.startsWith("pat-")
10
    ? await fetch(
11
        "https://api.hubapi.com/oauth/v2/private-apps/get/access-token-info",
12
        {
13
          method: "POST",
14
          headers: {
15
            Authorization: `Bearer ${auth.token}`,
16
            "Content-Type": "application/json",
17
            Accept: "application/json",
18
          },
19
          body: JSON.stringify({ tokenKey: auth.token }),
20
        }
21
      )
22
    : await fetch(
23
        `https://api.hubapi.com/oauth/v1/access-tokens/${auth.token}`,
24
        { headers: { Accept: "application/json" } }
25
      )
26

27
  if (!response.ok) {
28
    throw new Error(`${response.status} ${await response.text()}`)
29
  }
30

31
  return await response.json()
32
}
33