|
| 1 | +--- |
| 2 | +title: Machine tokens |
| 3 | +description: Learn about machine tokens and how to validate them in your backend. |
| 4 | +--- |
| 5 | + |
| 6 | +When you want a machine to authenticate requests to your backend, you authorize the requests with machine tokens. |
| 7 | + |
| 8 | +Machine tokens are JWTs that contain information about your machine. |
| 9 | + |
| 10 | +## Customizing your machine tokens. |
| 11 | + |
| 12 | +### Machine ID |
| 13 | + |
| 14 | +Every machine token you create needs to be associated with a `machine_id`. You can pick any value for the `machine_id` as long as it meets the following requirements: |
| 15 | + |
| 16 | +- It must be prefixed with `mch_` |
| 17 | +- It must only contain lowercase letters and numbers |
| 18 | + |
| 19 | +> [!TIP] |
| 20 | +> It is a good idea to have the `machine_id` correspond with the identity of the service generating the token. For example if you have a cron service, a `machine_id` of `mch_cron` would make sense. |
| 21 | +
|
| 22 | +### Some **valid** machine\_ids |
| 23 | + |
| 24 | +- mch\_cron |
| 25 | +- mch\_pub\_sub |
| 26 | +- mch\_scheduler |
| 27 | +- mch\_device\_ada3f8b7\_d491\_4fe4\_b76e\_99e4c00b56d1 |
| 28 | + |
| 29 | +#### Some invalid machine\_ids |
| 30 | + |
| 31 | +- user\_1234 |
| 32 | +- mch\_OH\_HI |
| 33 | +- MCH\_123 |
| 34 | +- mch-123 |
| 35 | + |
| 36 | +### Claims |
| 37 | + |
| 38 | +You can add custom claims to your machine token to include any additional information that your application might need. Claims are key-value pairs included in the token's payload, and they can convey important data such as permissions, roles, or any other attributes relevant to the machine's identity. |
| 39 | + |
| 40 | +For example, it is a good practice to include any permissions that your service requires directly in the claims. This allows your backend to easily verify what actions the machine is authorized to perform. |
| 41 | + |
| 42 | +> [!NOTE] |
| 43 | +> You cannot add claims that are [already set by clerk](#default-machine-claims). |
| 44 | +
|
| 45 | +### Expires In Seconds |
| 46 | + |
| 47 | +The `expiresInSeconds` parameter defines how long the machine token remains valid, specified in seconds. This parameter is optional and defaults to 60 seconds (1 minute). |
| 48 | + |
| 49 | +If you need the machine token to be valid for a longer period of time, you can set the `expiresInSeconds` parameter to a higher value. However, keep in mind that longer-lived tokens can present a higher security risk if compromised, while shorter-lived tokens may require more frequent token generation, potentially impacting your [Backend API rate limits](/docs/backend-requests/resources/rate-limits). Therefore, it's important to balance token lifespan with security requirements and rate limit considerations. |
| 50 | + |
| 51 | +### Clock Skew |
| 52 | + |
| 53 | +The `allowedClockSkew` parameter provides a leeway in seconds to account for clock differences between servers. This setting affects the `nbf` (Not Before) claim in the token, calculated as `nbf = current_time - allowed_clock_skew`. The default value is 5 seconds. |
| 54 | + |
| 55 | +Adjusting the clock skew helps prevent token validation failures due to minor time discrepancies between the issuing server and the verifying server. |
| 56 | + |
| 57 | +## Default machine claims |
| 58 | + |
| 59 | +Every generated token has default claims that cannot be overridden by custom claims. Clerk's default claims include: |
| 60 | + |
| 61 | +- `exp`: expiration time - the time after which the token will expire, as a Unix timestamp. Determined using the **Token Expires In Seconds** request body parameter when creating machine tokens. See [RFC 7519](https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.4) for more information. |
| 62 | +- `iat`: issued at - the time at which the token was issued as a Unix timestamp. For example: `1516239022`. See [RFC 7519](https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.6) for more information. |
| 63 | +- `jti`: JWT ID - the ID of the token internally generated by Clerk. For example: `a1b2c3d4e5f67890abcd`. See [RFC 7519](https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.7) for more information. |
| 64 | +- `iss`: issuer - the Frontend API URL of your instance. For example: `https://clerk.your-site.com` for a production instance or `https://your-site.clerk.accounts.dev` for a development instance. See [RFC 7519](https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.1) for more information. |
| 65 | +- `nbf`: not before - the time before which the token is considered invalid, as a Unix timestamp. Determined using the **Allowed Clock Skew** request body parameter when creating machine tokens. See [RFC 7519](https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.5) for more information. |
| 66 | +- `sub`: subject - the ID of the machine that created the token. Determined using the **Machine ID** request body parameter when creating machine tokens. For example: `mch_123`. See [RFC 7519](https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.2) for more information. |
| 67 | + |
| 68 | +## Making Machine Requests |
| 69 | + |
| 70 | +To start making machine requests, refer to [making machine requests](/docs/backend-requests/making/machine). |
| 71 | + |
| 72 | +## Validating Machine Tokens |
| 73 | + |
| 74 | +To learn how to manually verify a machine token, refer to [validating machine tokens](/docs/backend-requests/handling/manual-jwt#machine-tokens). |
0 commit comments