---
title: Connect your product to your users’ Turn Zero accounts
description: Let each user connect their Turn Zero account so your product can deploy projects there.
slug: /guides/connect-your-product-to-turn-zero
order: 65
visibility: public
redirects_from: [/guides/connect-your-product-to-your-users-turn-zero-accounts]
---

# Connect your product to your users’ Turn Zero accounts

Let users of your app builder, AI tool, or publishing platform deploy projects to their own Turn Zero accounts. Their applications count toward the usage and limits of the accounts they connect.

The connection gives your product access to the user's whole Turn Zero account. Use it only for the projects they choose to manage through your product.

Start by [registering your product](#register-your-product-once). You'll need a reachable HTTPS callback on your product's domain, a test project the signed-in user owns, and a Turn Zero account that can approve the connection.

The example uses a web product with a backend to publish a small public site. Replace `https://deploy.example/turn-zero/callback` with your callback in every example.

The `curl` examples show the requests your backend sends. In production, use your backend's HTTP client to keep tokens out of shell arguments. Replace values in angle brackets with your own values. Most responses show only the fields the guide uses.

When trying the examples by hand, enter secret values at a prompt that hides what you type. In your backend, use a JSON encoder to build request bodies, including bodies with names supplied by users.

Your project has two actions, **Connect** and **Publish**. The callback runs between them.

1. **Connect** starts an authorization attempt and sends the user to Turn Zero.
2. **Callback** checks the attempt against the user signed in to your product in that browser. It saves the token pair and returns the user to the project with **Connected** visible.
3. **Publish** runs when the user chooses it. It creates or reuses an application, then deploys the project.

## Register your product once

Send your callback address to the Turn Zero OAuth registration endpoint. Store the returned `client_id` with your product’s configuration and reuse it for every user who connects. The registration uses PKCE and has no client secret.

```sh
curl -sS https://turnzero.ai/oauth/register \
  -H 'Content-Type: application/json' \
  --data '{
    "client_name": "Project Publisher",
    "redirect_uris": ["https://deploy.example/turn-zero/callback"],
    "token_endpoint_auth_method": "none"
  }'
```

HTTP 201

```json
{
  "client_id": "<client id>",
  "redirect_uris": ["https://deploy.example/turn-zero/callback"],
  "token_endpoint_auth_method": "none",
  "client_name": "Project Publisher"
}
```

If Turn Zero no longer recognizes the registration, register again and have the user reconnect. Avoid registering a new client for each user or each deployment.

## Send the user to Turn Zero to approve the connection

For each connection attempt, create a random `state` and a separate random PKCE verifier. The verifier proves that the backend exchanging the code started the request. On your backend, save the signed-in user’s ID, the originating project ID, the state, and the verifier with the pending attempt. Make the attempt short-lived and use it once.

This Node.js example builds the address your backend sends the user to. Set `TZ_CLIENT_ID` to the value from registration. The OAuth request names the MCP address as its resource. The returned access token also works on the HTTP action API used below.

```js
import { createHash, randomBytes } from "node:crypto";

const state = randomBytes(32).toString("base64url");
const verifier = randomBytes(32).toString("base64url");
const challenge = createHash("sha256").update(verifier).digest("base64url");
const redirectUri = "https://deploy.example/turn-zero/callback";

const authorization = new URL("https://turnzero.ai/oauth/authorize");
authorization.search = new URLSearchParams({
  response_type: "code",
  client_id: process.env.TZ_CLIENT_ID,
  redirect_uri: redirectUri,
  resource: "https://turnzero.ai/mcp",
  scope: "tzcloud",
  state,
  code_challenge: challenge,
  code_challenge_method: "S256",
}).toString();

// Save this user's ID, the originating project ID, state and verifier for the attempt.
// Send the user's browser to authorization.href.
```

If the user needs to sign in, Turn Zero asks them to do so. Someone without an account can create one when sign-ups are open. Otherwise, they need an invitation. The approval screen names the hostname of your callback, for example **Allow deploy.example to use your Turn Zero account?** It also shows which account is signed in. A browser already signed in recently can go straight to approval.

Send the user's browser tab to the authorization address. Keep Turn Zero sign-in on its own page. Keep that page out of frames and embedded browsers, and keep Turn Zero email codes and passkeys out of your product's sign-in form.

When the user allows access, Turn Zero redirects the browser to your callback:

```http
Location: https://deploy.example/turn-zero/callback?code=EXAMPLE_CODE&state=EXAMPLE_STATE
```

Handle the callback only in a browser signed in to your product. Find the pending attempt that matches the returned `state`. Check that the signed-in user is the same user saved with the attempt. Use the project saved with that attempt.

If no one is signed in, the users differ, or the attempt is missing, expired, or already used, show an error and stop before exchanging the code or saving anything.

If the verified callback contains `error=access_denied`, clear the attempt and return to the project with **Connect** available again. Leave any existing connection in place.

Mark the attempt used before you send the code for exchange. Make that update succeed for only one callback.

Keep the callback's query string out of logs. After the exchange, redirect to the project page so the code is out of the address bar.

## Exchange the code and keep the token pair on your backend

The callback handler sends `code`, the saved verifier, and the same callback address. The examples use `TZ_CODE`, `TZ_VERIFIER`, and `TZ_CLIENT_ID` for those values.

```sh
curl -sS https://turnzero.ai/oauth/token \
  --data-urlencode 'grant_type=authorization_code' \
  --data-urlencode "client_id=$TZ_CLIENT_ID" \
  --data-urlencode "code=$TZ_CODE" \
  --data-urlencode "code_verifier=$TZ_VERIFIER" \
  --data-urlencode 'redirect_uri=https://deploy.example/turn-zero/callback' \
  --data-urlencode 'resource=https://turnzero.ai/mcp'
```

HTTP 200

```json
{
  "access_token": "<access token>",
  "token_type": "bearer",
  "expires_in": 2592000,
  "refresh_token": "<refresh token>",
  "scope": "tzcloud"
}
```

The authorization code is single-use and expires after 60 seconds. If its exchange fails and you have no saved token pair, start a new connection attempt. An access token lasts 30 days. Its refresh token lasts 60 days.

Read the Turn Zero account ID as described in [Keep each project with its connection](#keep-each-project-with-its-connection). Save that ID and the token pair for this user in your backend's encrypted secret storage. Limit token access to the backend services that use them. Keep both tokens out of the browser, logs, project files, and deployed applications.

After saving the pair, clear the pending attempt and return the user to the originating project. Show **Connected**. Check these three cases before adding deployment.

1. Approve the connection. Check that you return to the project with **Connected** visible.
2. On a separate test project with no connection, start an attempt and cancel it. Check that **Connect** is available again. Cancelling a reconnect should leave any existing connection in place.
3. Use a test attempt to check the callback in two cases. Sign in as a different test user in one case and stay signed out in the other. In both cases, check that the backend skips the code exchange and saves no tokens.

All three checks leave the project unpublished.

Before the access token expires, renew with the saved refresh token. Here `TZ_REFRESH_TOKEN` is the current refresh token.

```sh
curl -sS https://turnzero.ai/oauth/token \
  --data-urlencode 'grant_type=refresh_token' \
  --data-urlencode "client_id=$TZ_CLIENT_ID" \
  --data-urlencode "refresh_token=$TZ_REFRESH_TOKEN" \
  --data-urlencode 'resource=https://turnzero.ai/mcp'
```

HTTP 200 with a replacement pair

```json
{
  "access_token": "<new access token>",
  "token_type": "bearer",
  "expires_in": 2592000,
  "refresh_token": "<new refresh token>",
  "scope": "tzcloud"
}
```

Save both replacements together. Allow one refresh at a time for each connection. If the response is lost, the same client can retry a used refresh token within ten minutes of its first use. The retry returns a new pair only while the connection is active and the used token has not expired. If the same client presents it after ten minutes, Turn Zero ends the connection and invalidates its access and refresh tokens. The user must connect again. See [Sign-in, sessions, and tokens](/cloud/concepts/sign-in-sessions-and-tokens/#the-tool-s-connection) for details.

If a connection ends and neither you nor the user ended it, someone may have copied the tokens. Check where your backend stores them, who can read them, and whether they reached any logs.

The `tzcloud` access token also works at `https://turnzero.ai/mcp`, the address it was issued for.

## Keep each project with its connection

The two customers below use your product’s one registered client ID. Each has a separate connection and application.

| Signed-in user | Project | Saved connection | Turn Zero application ID |
| --- | --- | --- | --- |
| First customer | Portfolio | First customer’s connection | App ID from publishing Portfolio |
| Second customer | Recipe site | Second customer’s connection | App ID from publishing Recipe site |

After exchanging the code, send a `GET` request to `https://turnzero.ai/api/v1/read_account` with the new access token as a bearer token. Save only `account.id` from the response, with the connection. The response also includes the account's sign-in address, so keep the response out of logs.

Each time someone chooses **Publish**, check that they own the project. Load its saved application ID and the Turn Zero account ID that goes with it. Reuse the application ID only when that account ID matches the one on the current connection.

If the IDs differ or the saved application's account is unknown, offer to reconnect the original account or publish a new application in the connected account. Keep the original application ID with the original account.

## Create an application and save its manifest

Run these requests when the user chooses **Publish**. Run one publish attempt at a time for each project. Use the project’s connection access token as `TZ_ACCESS_TOKEN`. The example creates a serverless application for a public static site. Make the public destination clear before the user confirms. A new application starts with only a production environment.

```sh
curl -sS https://tzcloud.turnzero.ai/v1/create_application \
  -H "Authorization: Bearer $TZ_ACCESS_TOKEN" \
  -H 'Content-Type: application/json' \
  --data '{"name":"My project","kind":"serverless"}'
```

Response excerpt

```json
{
  "contract_version": 1,
  "outcome": "created",
  "application": { "id": "<application id>" }
}
```

Save `application.id` and the connected Turn Zero account ID with the project. Use the application ID as `TZ_APP_ID`.

### Submit the manifest

Submit the whole manifest before the first deploy and whenever its declarations change. This manifest makes the example site public and adds no services.

```sh
curl -sS https://tzcloud.turnzero.ai/v1/submit_manifest \
  -H "Authorization: Bearer $TZ_ACCESS_TOKEN" \
  -H 'Content-Type: application/json' \
  --data @- <<JSON
{
  "application": "$TZ_APP_ID",
  "manifest": {
    "manifest_version": 1,
    "region": "usa",
    "services": [],
    "egress": [],
    "audience": { "kind": "public" },
    "packages": []
  }
}
JSON
```

Response excerpt

```json
{
  "contract_version": 1,
  "outcome": "recorded",
  "application": "<application id>"
}
```

The manifest holds service declarations and ordinary settings. To give a project a secret, store the value separately in Turn Zero at the application and environment that use it and refer to the stored name in the manifest. Keep secret values out of manifest values, source uploads, and logs. Follow [Store a secret](/cloud/getting-started/store-a-secret/) for the value-entry steps. The connection’s OAuth tokens stay in your product’s secret storage.

## Upload the files and watch the deployment

For a small project, send the complete version in one `deploy_files` request. The API keeps the files and starts the deploy. This site has one file. Set `environment` explicitly.

```sh
curl -sS https://tzcloud.turnzero.ai/v1/deploy_files \
  -H "Authorization: Bearer $TZ_ACCESS_TOKEN" \
  -H 'Content-Type: application/json' \
  --data @- <<JSON
{
  "application": "$TZ_APP_ID",
  "environment": "production",
  "files": [
    {
      "path": "client/index.html",
      "content": "<!doctype html><meta charset=utf-8><title>My project</title><h1>Hello from my project</h1>"
    }
  ]
}
JSON
```

HTTP 202, response excerpt

```json
{
  "contract_version": 1,
  "application": "<application id>",
  "environment": "production",
  "state": "deploying",
  "version": 1
}
```

### Check the deployment

Save the returned `version`. Call `read_status` with the same application ID and environment to check which version is serving.

```sh
curl -sS https://tzcloud.turnzero.ai/v1/read_status \
  -H "Authorization: Bearer $TZ_ACCESS_TOKEN" \
  -H 'Content-Type: application/json' \
  --data @- <<JSON
{
  "application": "$TZ_APP_ID",
  "environment": "production",
  "wait_seconds": 30
}
JSON
```

HTTP 200, response excerpt after the version starts serving

```json
{
  "contract_version": 1,
  "settled": true,
  "application": {
    "id": "<application id>",
    "environment": "production",
    "state": "deployed",
    "version": 1,
    "hostname": "<assigned hostname>",
    "environments": {
      "production": {
        "deploy": { "version": 1, "state": "deployed" }
      }
    }
  }
}
```

If `settled` is false, read again after a short wait. Compare the requested version with `application.version` and the deploy record at `application.environments.production.deploy`. Report success when that version serves and its deploy state is `deployed`.

Link to the site using `application.hostname`. To check the example, open the link and look for **Hello from my project**.

A failed deployment can leave the previous version serving. Here version 2 failed and version 1 still serves.

```json
{
  "contract_version": 1,
  "settled": true,
  "application": {
    "id": "<application id>",
    "environment": "production",
    "state": "deployed",
    "version": 1,
    "environments": {
      "production": {
        "deploy": { "version": 2, "state": "failed" }
      }
    }
  }
}
```

Stop polling that attempt once its deploy record reaches its final state. Tell the user **Version 2 failed to publish. Version 1 is still live.** If the upload request times out or the connection drops before you receive an answer, read status before retrying. Retry the file request only when status confirms it started no deploy.

For projects that exceed the inline file limits, see [deploy_files](/cloud/reference/actions/deploy-files/) and [Ways to deploy](/cloud/getting-started/ways-to-deploy/) for larger uploads. Read [Size and usage](/cloud/guides/plan-and-usage/) to understand the connected account’s limits and charges. Your product shares those limits with the user’s other projects in that account.

## Send the user to browser approval when an action requires it

A connection lets your product request actions on the user's behalf. Some actions, such as deleting an application, wait for the user to approve them in a browser. Send the delete request only after the user asks to delete the application and you confirm that they own the project. In production, save a `request_id` and include it in the deletion request. Set `TZ_REQUEST_ID` to the saved ID before running the example. If you have the pending action ID, read its outcome before requesting another deletion.

```sh
curl -sS https://tzcloud.turnzero.ai/v1/delete_application \
  -H "Authorization: Bearer $TZ_ACCESS_TOKEN" \
  -H 'Content-Type: application/json' \
  --data "{\"application\":\"$TZ_APP_ID\",\"request_id\":\"$TZ_REQUEST_ID\"}"
```

HTTP 202, response excerpt

```json
{
  "contract_version": 1,
  "pending_action": { "id": "<pending action id>", "state": "requested" },
  "approval_url": "<approval URL>"
}
```

Open the `approval_url` from the response in the user’s browser. Treat the action as pending until [read_approval_status](/cloud/reference/actions/read-approval-status/) returns the result. Set `TZ_PENDING_ID` to the returned `pending_action.id`.

```sh
curl -sS https://turnzero.ai/api/v1/read_approval_status \
  -H "Authorization: Bearer $TZ_ACCESS_TOKEN" \
  -H 'Content-Type: application/json' \
  --data "{\"id\":\"$TZ_PENDING_ID\"}"
```

Response excerpt

```json
{
  "contract_version": 1,
  "pending_action": { "id": "<pending action id>", "state": "completed" }
}
```

Use the returned record’s `state` and `outcome`. The action is still in progress while its state is `requested`, `approved`, or `executing`. Report the final result when it is `completed`, `failed`, `declined`, or `expired`. A link opening in the browser does not confirm approval.

## Refusals

| Code or result | Cause | Remedy |
| --- | --- | --- |
| `invalid_client_metadata` | Turn Zero refused the product registration's callback or client settings. | Use your product's HTTPS callback and set `token_endpoint_auth_method` to `none`. |
| `invalid_request` at authorization | The client ID is missing, the callback does not match the registration, or the request does not use a code with S256 PKCE. | Use the saved client ID and exact registered callback. Send `response_type=code` and an S256 challenge. |
| `access_denied` on the callback | The user did not grant access. | Verify the attempt and the signed-in user, clear the attempt, and show **Connect**. Keep any connection they already saved. |
| `invalid_client` at authorization, token exchange or refresh | Turn Zero no longer recognizes the registration. | Register again and have the user reconnect. |
| `invalid_grant` at token exchange or refresh | Turn Zero could not accept the code or refresh token. | Check that the code exchange uses the same callback and saved verifier. If the code is spent or expired, or the connection has ended, start a new connection attempt. |
| HTTP 401 with `authentication_required` on an action call | Turn Zero did not accept the access token. It may have expired or the connection may have ended. | Try refreshing once. If refresh succeeds, save the new pair and retry the action. If the connection has ended, clear the saved tokens and show **Connect**. |
| `name_taken` | The connected account already has a live application with that name. | Choose a different name. For a project that already has a confirmed application in this account, reuse its saved ID. |
| `manifest_invalid` or `manifest_missing` | The manifest was refused or the application has no recorded manifest. | Fix any fields named in the refusal. Submit the whole manifest before retrying the deploy. |
| `deploy_files_too_large` | The file list exceeds an upload limit. | Read the returned limit and use a larger-upload route from [Ways to deploy](/cloud/getting-started/ways-to-deploy/) when the inline limit is the problem. |
| HTTP 202 with `pending_action.state: requested` | A request such as deletion is waiting for browser approval. | Open the returned `approval_url` for the user and read the outcome with `read_approval_status`. |
| `unsupported_token_type` at disconnect | The token is of a kind that `/oauth/revoke` cannot revoke. | Send the OAuth refresh token saved for this connection, or direct the user to **AI access** in Turn Zero. |
| `revoked: false` | The request ended no live connection. | Stop using the saved tokens. Show the user where to check and end the connection in **AI access**. |

## Disconnect the account

The user can end the connection from **AI access** in the Turn Zero dashboard. See [Read and end connections](https://turnzero.ai/turnzero/guides/your-account/#4-read-and-end-connections). This leaves the user’s projects in their account.

Give the user a disconnect control. When the product no longer needs a connection, send the current refresh token to Turn Zero to revoke it before clearing your stored tokens.

Do this when the user disconnects or deletes their account in your product, and when you stop offering the integration. If a user deletes a project, revoke the connection when no remaining project uses it. Deleting your stored copy alone leaves the connection active at Turn Zero until it expires.

```sh
curl -sS https://turnzero.ai/oauth/revoke \
  --data-urlencode "client_id=$TZ_CLIENT_ID" \
  --data-urlencode "token=$TZ_REFRESH_TOKEN"
```

HTTP 200

```json
{ "contract_version": 1, "revoked": true }
```

A response with `revoked: false` means this request did not revoke a live connection.

If the revoke request fails, stop making action calls with the connection. Keep the refresh token in encrypted storage only to retry revocation before clearing it. If you can still reach the user, show them how to end the connection in **AI access**.
