Sign in from a script #
On this page
Prompt:
Give this script a token for one of Turn Zero's own products without pasting one into the chat.
Also works:
- "Sign the cron job in to the product from a terminal."
- "I have no MCP client on this machine. How does a script get a token?"
- "Print me a code I can approve in the browser."
A script or AI assistant on a computer with no browser can sign in using a code. It prints a link and code for you to approve in a browser on another computer. For the shared Turn Zero connection, see Use the Turn Zero connection. The steps below are for a product's own server and return a product token. Neither route authorizes Cloud management calls; use Mint a token from an ordinary signed-in connection for those.
What your AI does #
- Registers the script at
/oauth/registeras a client with no secret, under a name you will recognise on the approval page (step 1). - Requests the codes at
/oauth/device_authorization, naming the product's endpoint as the resource (step 2). - Prints the code and the link, and opens the link in a browser where its computer has one. It asks you to sign in and allow the request (step 3).
- Polls
/oauth/tokenevery five seconds under the device-code grant until the response contains the token (step 4). To poll is to ask again at a fixed interval. - Reads the poll's answer without printing it. Writes the token into the script's secret store, never into a file it commits or into the chat.
- Asks you for the product's endpoint address where the prompt does not name the product.
What you need #
- A Turn Zero account that has the product. Depending on the product, the approval page refuses an account without it, or the account is checked after you allow the sign-in and the poll then answers
invalid_grant. - The product's MCP endpoint address, which the product's own documentation gives. Every request names that address as its
resource. - A browser on any computer, for the one approval. It need not be the computer the script runs on.
Before your AI starts #
For your AI: Check the following before starting.
- The platform address:
https://dev.turnzero.ai. - For the product-token steps below, use the product's endpoint address as
resource. For the shared Turn Zero connection, usehttps://dev.turnzero.ai/mcpand follow Use the Turn Zero connection instead. - A place the script keeps the token, such as an environment variable set from a secret store.
Use the Turn Zero connection #
An MCP client that supports device sign-in can connect to https://dev.turnzero.ai/mcp without a browser on its own computer. It requests a code at /oauth/device_authorization, naming that address as resource. You open the returned link on another computer, check that the code and client name match, and approve the sign-in. The client polls /oauth/token for the result.
This returns an access token and a refresh token for a limited session, rather than the product token used in the steps below. The client must keep both credentials in a protected store and handle renewal, as The tool's connection describes. Never paste them into chat.
This is an account connection. For an app user's sign-in, use that application's own environment. This connection can use tzMesh and tzTrack when your account has access, along with a few account and documentation tools. It cannot manage or deploy Cloud applications, mint tokens, or use Turn Zero's storage or outbound-request services. These limits remain after renewal and also apply to platform staff. Device sign-in limits gives the details. A destructive action still needs your approval in a browser.
Steps #
1. Register the script as a client #
Every program that uses the sign-in registers itself. Nobody hands out a client ID by hand. The script lists the device grant as its only grant_types and gives no redirect address, because the device grant redirects nothing. The response contains a client_id, which the script sends in every later request.
curl -s -X POST https://dev.turnzero.ai/oauth/register -H 'content-type: application/json' -d '{"grant_types":["urn:ietf:params:oauth:grant-type:device_code"],"client_name":"My script"}'
The platform deletes a registration a day after nothing refers to it. The registration stays if a token issued to it has yet to be revoked, even after the token expires. Use the same client_id if it is still registered. If requesting codes returns invalid_client, register again first.
2. Request the codes #
The request names the client and gives the product's endpoint as resource. It may also name the product's scope; leaving the scope out selects that same scope. The response contains device_code, which the script keeps secret. It also contains user_code, which the script prints.
The response also includes verification_uri, the approval page's address, and verification_uri_complete, the same address with the code filled in. Last come expires_in of 600 seconds and interval of 5.
With command tracing off, this POSIX shell example uses jq to read the response, keep device_code for step 4, and print only the user code and the complete link on success.
device_response=$(curl -sS -X POST https://dev.turnzero.ai/oauth/device_authorization \
--data-urlencode 'client_id=<client_id>' --data-urlencode 'resource=<product endpoint>') || exit 1
if ! device_code=$(printf '%s' "$device_response" | jq -er '.device_code | strings | select(length > 0)'); then
printf '%s' "$device_response" | jq -er '.error | strings | select(length > 0)' >&2
exit 1
fi
user_code=$(printf '%s' "$device_response" | jq -er '.user_code') || exit 1
verification_link=$(printf '%s' "$device_response" | jq -er '.verification_uri_complete') || exit 1
unset device_response
printf '%s\n%s\n' "$user_code" "$verification_link"
3. Open the link and allow it #
The script prints the code and the complete link. Where its computer has a browser, it also opens the link there: open on macOS, start on Windows, or xdg-open on Linux. Otherwise, open the link in a browser on any computer. Or open https://dev.turnzero.ai/oauth/device and type the code there.
Sign in to Turn Zero as you do on the website. The page then shows the code and the script's registered name, and asks whether to allow it. Check that the code matches the one the script printed, then press Allow access. The request lasts ten minutes. If the page refuses the account you signed in with, press Cancel. The next poll that waits the required interval gets the cancellation if the request is still within its ten minutes.
4. Poll for the token #
Once the request is made, the script asks the token endpoint whether you allowed it. It polls at most once every five seconds, with the same client_id.
In a POSIX shell with command tracing off, keep the code from step 2 in device_code and capture one poll's response in poll_response. The pipe keeps the device code out of the command arguments.
poll_response=$(printf '%s' "$device_code" | curl -s -X POST https://dev.turnzero.ai/oauth/token \
-d grant_type=urn:ietf:params:oauth:grant-type:device_code -d 'client_id=<client_id>' \
--data-urlencode 'device_code@-')
The script parses poll_response without printing it. On success, it writes the token into its secret store, then clears the variable.
Until you allow the request, the answer is {"error":"authorization_pending"}, status 400. A poll sooner than five seconds after the last one gets slow_down, also at 400. A refused poll counts too: wait five seconds from that poll before trying again. The first poll after the ten minutes gets expired_token at 400 and deletes the request. Later polls get invalid_grant; start the sign-in again from step 2.
After you cancel the request, the next poll that waits the required interval receives 400 access_denied while the request is still live. This also applies if you cancelled on the page that refused your account. A poll that comes too soon can get slow_down, and an expired request gets expired_token first. If you leave a refused request alone, it answers authorization_pending until its ten minutes run out.
Once you allow the request, the next poll gets the token, once. The token is in the answer's access_token, with expires_at, resource, and token_id. A second poll with the same code is refused, so the script keeps the value from that one answer.
A new product sign-in can replace an earlier token issued to the same registered client under the same label. The response includes replaced. Save the new value in the script's secret store and retire the old copy when it is replaced.
5. Use the token #
Send the token as a bearer token to the product's own endpoint, as the product's documentation says. On this platform, the token can call list_tokens, the public reads, and the account reads allowed by the product's grant. It lasts 180 days and is not renewed, so run the script's sign-in again when it expires. A fresh sign-in can replace the earlier token issued to this client under the same label. To end it early, call revoke_token with the token's token_id as id.
Expected result #
The script prints a code such as BCDF-GHJK and a link. After you allow the request, the browser confirms your approval. The script checks its next poll for a token or refusal. On success, it stores the token securely without printing it, and list_tokens lists it under the script's registered name. A second poll is refused with invalid_grant.
Refusals #
See the refusals reference for other platform errors.
| Refusal | Status | Cause | Remedy |
|---|---|---|---|
invalid_target |
400 | The resource is not a product endpoint the device grant serves, or the request named none. |
Name the product's MCP endpoint exactly, as its documentation gives it. |
invalid_scope |
400 | The request asked for a scope that is not the product's. | Leave scope out, or name the product's scope. |
invalid_client_metadata |
400 | The registration gave no redirect and did not list the device grant as its only grant_types. Or it gave a redirect that is not an HTTPS or loopback URL, or a token_endpoint_auth_method other than none. |
Register as step 1 shows. |
invalid_client |
401 | No registration has this client_id, for example because the daily cleanup deleted it. |
Register again with step 1, then start the sign-in again. |
invalid_grant |
400 | The device code may be unknown or belong to another client. The request may also have already received its final response. Or the sign-in ended before the poll. Or, for a product that checks the account after you allow the sign-in, the account does not have the product. | Start the sign-in again from step 2, signed in with the account that has the product. |
account_suspended |
403 | The account was suspended before the token exchange. No token is issued. | After an operator reinstates the account, start the sign-in again from step 2. |
A poll can also get four answers while the request is open: authorization_pending, slow_down, expired_token, and access_denied. These are not in the table. Step 4 says what each means, and the script handles each as that step says.
Related #
- Sign-in, sessions, and tokens compares every credential and says what ends a product token.
- Mint a token mints a token for a script that calls this platform's own actions.
- Connect your AI assistant describes the sign-in an MCP client runs for you.
- Glossary defines the terms used on this page.