Skip to content
Source
Sign inSign up

Ask the person for a token

CLI and APIs

Ask the person for a token

Your tool needs a token, and you do not want the person to copy one by hand. Ask for it with the device login: the person checks the request in the browser and creates the token, and your tool gets it.

2 min read

1. Start the request

The request needs no token:

curl -X POST -H "Content-Type: application/json" \
  -d '{"client_name": "my-tool", "token": {"name": "my-tool", "repos": ["acme/app"], "actions": ["contents:read", "pulls:read", "pulls:create"], "ttl": 7200}}' \
  https://source.tanstack.com/api/v1/login/device/code

The answer:

{
  "device_code": "<secret>",
  "user_code": "BCDF-GHJK",
  "verification_uri": "https://source.tanstack.com/login/device",
  "verification_uri_complete": "https://source.tanstack.com/settings/tokens/new?code=BCDF-GHJK",
  "expires_in": 900,
  "interval": 5
}

The fields of token are in the fields of the token request.

Show the person verification_uri_complete. They can change the token there, then click Create token or Deny.

The approval page: the one-time code RWMD-XVST, the token name proof run, the Write access and the repository proofuser/token-demo

3. Poll for the token

Every interval seconds, poll with the device_code:

curl -X POST -H "Content-Type: application/json" \
  -d '{"device_code": "<secret>"}' \
  https://source.tanstack.com/api/v1/login/device/token

4. Read the answer of the poll

AnswerWhat it means
200 with {"access_token": "tsp_...", "token_type": "bearer"}The token. Only the first poll after the approval gets it.
400 with authorization_pendingThe person did not answer yet. Poll again.
400 with slow_downYou poll too fast. Wait 5 seconds more between polls.
400 with access_denied or expired_tokenThe person clicked Deny, or 15 minutes passed. Start again.

Done

Your tool has the token that the person created. It can differ from your request, because the person can change the repositories, the actions and the end time.

The fields of the token request

FieldWhat it is
nameThe name of the token in the list of the person.
reposThe repositories, as owner/name. At most 20.
actionsThe actions, for example contents:write or vault:read. The Advanced part of the token form lists them all.
ttlHow long the token lives, in seconds. The default is 3600, and the most is 365 days.