Create a token

Create API tokens using the InfluxDB user interface (UI), the influx command line interface (CLI), or the InfluxDB API.

To follow best practices for secure API token generation and retrieval, InfluxDB enforces access restrictions on API tokens.

  • Tokens are visible to the user who created the token.
  • InfluxDB only allows access to the API token value immediately after the token is created.
  • You can’t change access (read/write) permissions for an API token after it’s created.
  • Tokens stop working when the user who created the token is deleted.

We recommend the following for managing your tokens:

Manage tokens in the InfluxDB UI

To manage InfluxDB API Tokens in the InfluxDB UI, navigate to the API Tokens management page.

In the navigation menu on the left, select Data (Load Data) > API Tokens.

Load Data

Create a token in the InfluxDB UI

  1. From the API Tokens management page, click Generate and select a token type (Read/Write Token or All Access API Token).
  2. In the window that appears, enter a description for your token in the Description field.
  3. If generating a read/write token:
    • Search for and select buckets to read from in the Read pane.
    • Search for and select buckets to write to in the Write pane.
  4. Click Save.

Create a token using the influx CLI

Use the influx auth create command to create a token. Include flags with the command to grant specific permissions to the token. See the available flags. Only tokens with the write: authorizations permission can create tokens.

  1. # Syntax
  2. influx auth create -o <org-name> [permission-flags]

Examples

Create an All Access token

Create an All Access token to grant permissions to all resources in an organization.

  1. influx auth create \
  2. --org my-org \
  3. --all-access

Create an operator token

Create an operator token to grant permissions to all resources in all organizations.

  1. influx auth create \
  2. --org my-org \
  3. --operator

To view or create an operator token with the InfluxDB UI, api/v2 API, or influx CLI after the setup process is completed, you must use an existing operator token.

To create a new operator token without using an existing one, see how to use the influxd recovery auth CLI.

Create a token with specified permissions

Create a token with specified read permissions
  1. influx auth create \
  2. --org my-org \
  3. --read-bucket 03a2bbf46309a000 \
  4. --read-bucket 3a87c03ace269000 \
  5. --read-dashboards \
  6. --read-tasks \
  7. --read-telegrafs \
  8. --read-user
Create a token scoped to a user and with specified read and write permissions
  1. influx auth create \
  2. --org ORG_NAME \
  3. --user USERNAME \
  4. --read-authorizations \
  5. --write-authorizations \
  6. --read-buckets \
  7. --write-buckets \
  8. --read-dashboards \
  9. --write-dashboards \
  10. --read-tasks \
  11. --write-tasks \
  12. --read-telegrafs \
  13. --write-telegrafs \
  14. --read-users \
  15. --write-users

See the influx auth create documentation for information about other available flags.

Create a token using the InfluxDB API

Use the /api/v2/authorizations InfluxDB API endpoint to create a token.

  1. POST http://localhost:8086/api/v2/authorizations

Include the following in your request:

RequirementInclude by
API token with the write: authorizations permissionUse the Authorization header and the Bearer or Token scheme.
OrganizationPass as orgID in the request body.
Permissions listPass as a permissions array in the request body.
  1. INFLUX_ORG_ID=YOUR_ORG_ID
  2. INFLUX_TOKEN=YOUR_API_TOKEN
  3. curl -v --request POST \
  4. http://localhost:8086/api/v2/authorizations \
  5. --header "Authorization: Token ${INFLUX_TOKEN}" \
  6. --header 'Content-type: application/json' \
  7. --data '{
  8. "status": "active",
  9. "description": "iot-center-device",
  10. "orgID": "'"${INFLUX_ORG_ID}"'",
  11. "permissions": [
  12. {
  13. "action": "read",
  14. "resource": {
  15. "orgID": "'"${INFLUX_ORG_ID}"'",
  16. "type": "authorizations"
  17. }
  18. },
  19. {
  20. "action": "read",
  21. "resource": {
  22. "orgID": "'"${INFLUX_ORG_ID}"'",
  23. "type": "buckets"
  24. }
  25. },
  26. {
  27. "action": "write",
  28. "resource": {
  29. "orgID": "'"${INFLUX_ORG_ID}"'",
  30. "type": "buckets",
  31. "name": "iot-center"
  32. }
  33. }
  34. ]
  35. }'

Create a token scoped to a user

To scope a token to a user other than the token creator, pass the userID property in the request body.

  1. ######################################################
  2. # The example below uses common command-line tools
  3. # `curl`, `jq` with the InfluxDB API to do the following:
  4. # 1. Create a user.
  5. # 2. Find the new or existing user by name.
  6. # 3. If the user exists:
  7. # a. Build an authorization object with the user ID.
  8. # b. Create the new authorization.
  9. # c. Return the new token.
  10. ######################################################
  11. INFLUX_ORG_ID=YOUR_ORG_ID
  12. INFLUX_TOKEN=YOUR_API_TOKEN
  13. function create_token_with_user() {
  14. curl --request POST \
  15. "http://localhost:8086/api/v2/users/" \
  16. --header "Authorization: Token ${INFLUX_TOKEN}" \
  17. --header 'Content-type: application/json' \
  18. --data "{\"name\": \"$1\"}"
  19. curl --request GET \
  20. "http://localhost:8086/api/v2/users?name=$1" \
  21. --header "Authorization: Token ${INFLUX_TOKEN}" \
  22. --header 'Content-type: application/json' | \
  23. jq --arg USER $1 '.users[0] // error("User missing")
  24. | {
  25. "orgID": "'"${INFLUX_ORG_ID}"'",
  26. "userID": .id,
  27. "description": $USER,
  28. "permissions": [
  29. {"action": "read", "resource": {"type": "buckets"}}
  30. ]
  31. }' | \
  32. curl --request POST \
  33. "http://localhost:8086/api/v2/authorizations" \
  34. --header "Authorization: Token ${INFLUX_TOKEN}" \
  35. --header 'Content-type: application/json' \
  36. --data @- | \
  37. jq '.token'
  38. }
  39. create_token_with_user 'iot_user_1'

See the POST /api/v2/authorizations documentation for more information about options.