JWTs

JSON Web Tokens (JWTs) are signed JSON objects that authenticate your API requests. After you authenticate with application tokens or user credentials, the API returns a JWT called auth_token.

Use the auth token

Include the auth_token in the Authorization header of every API request using the Bearer scheme:

1Authorization: Bearer YOUR_AUTH_TOKEN

The following example shows how to include the auth_token when creating a new user:

POST
/api/v1/user
1import requests
2
3url = "https://cdn.emnify.net/api/v1/user"
4
5payload = {
6 "username": "eabbot@flatland.org",
7 "name": "Edwin Abbott",
8 "organisation": { "id": 123 },
9 "roles": [{ "id": 1 }, { "id": 2 }]
10}
11headers = {
12 "Authorization": "Bearer <token>",
13 "Content-Type": "application/json"
14}
15
16response = requests.post(url, json=payload, headers=headers)
17
18print(response.json())

Don’t confuse auth_token with application_token. Application tokens are long-lived credentials you use to obtain an auth token. The auth token is what you include in API request headers.

JWT structure

A JWT consists of three parts separated by dots: header.payload.signature

PartDescription
HeaderContains the token type (JWT) and signing algorithm
PayloadContains claims about the user and token metadata
SignatureVerifies the token hasn’t been tampered with

Decode and inspect tokens

To inspect a JWT’s contents, use jwt.io or decode it in your application.

Never share your auth tokens publicly. If you use jwt.io, be aware that the token contents are visible and could be logged. For production debugging, decode tokens locally.

Example using Python:

1import base64
2import json
3
4def decode_jwt(token):
5 parts = token.split('.')
6 payload = parts[1]
7 # Add padding if needed
8 payload += '=' * (4 - len(payload) % 4)
9 decoded = base64.urlsafe_b64decode(payload)
10 return json.loads(decoded)

Handle expired tokens

When your auth_token expires, API requests return a 401 Unauthorized error. To handle this:

  1. Catch the 401 response in your application
  2. Re-authenticate to get a new token (using your application token or refresh token)
  3. Retry the failed request with the new token

To avoid failed requests, track token expiration and refresh proactively. For user credentials, the auth_token expires after 240 minutes.