# Add Connect to Sage to your app

Create a setup session with state and PKCE, send the customer to the code page and exchange the completed callback.

Your backend creates the setup session. The customer downloads Boost Plugins, approves a Sage company using the temporary code, then returns to your app. Portal administrators use their Boost sign-in; customers do not need that portal login to enter the code in Windows.

## Prepare your project

1. [Create or select a project](/docs/boost-connect-sage/quickstart) in the portal.

1. Register the exact HTTPS callback URL under **Projects → OAuth callback URLs**. Use one URL per line, with no wildcard or fragment.

1. [Create a project API key](/docs/boost-connect-sage/api-keys) with the access your integration needs and store it on your backend.

## Create a setup session

For each attempt, generate a cryptographically random `state` and a PKCE `codeVerifier`. Store them against the signed-in customer session. Create `codeChallenge` as the base64url-encoded SHA-256 digest of that verifier, without padding.

Send `POST https://sage-connector-public-api.nameless-union-7a77.workers.dev/v2/setup-sessions` with your API key in the Bearer header and a JSON body shaped like this UK read-only example:

```json
{
  "externalCustomerId": "customer-123",
  "displayName": "Example company",
  "redirectUri": "https://app.example.com/sage/callback",
  "state": "GENERATE_RANDOM_STATE_FOR_THIS_ATTEMPT",
  "codeChallenge": "GENERATE_S256_CHALLENGE_FROM_SAVED_VERIFIER",
  "operations": [
    "info.get",
    "uk.list",
    "uk.get"
  ]
}
```

The placeholder state and challenge are not valid credentials. Generate fresh values for every attempt. The verifier must contain 43–128 allowed PKCE characters; its S256 challenge is 43 base64url characters. Use `us.list` and `us.get` for US access, or request both editions' read operations if your project and key allow them.

The response returns `setupUrl` and `expiresAt` in Unix milliseconds. Send the customer to that URL. The desktop setup code is in its fragment; do not move it into a query string, log it or send it to analytics. The session expires after 15 minutes.

## Complete the callback

1. The customer follows [Connect a Sage company](/docs/boost-connect-sage/connect) and selects **Return to** your app after approval.

1. Require the original signed-in customer session and compare the returned `state` with the saved value. Reject a mismatch.

1. On your backend, call `POST /v2/setup-sessions/exchange` with the initiating project's API key, the returned `code` and original `codeVerifier`.

1. Verify that `externalCustomerId` in the exchange result matches the customer who started setup. Store the returned `connectionId` and `edition` against that customer.

1. Clear the pending verifier and state after successful exchange, then make a read request through the connection.

```json
{
  "code": "CALLBACK_CODE",
  "codeVerifier": "ORIGINAL_SAVED_PKCE_VERIFIER"
}
```

The callback code is single-use and differs from the code pasted into Windows. Exchange returns the approved connection identity, not an OAuth API access token. Your backend continues to use its API key; the Windows connector manages its own device credentials.

## Handle cancelled or expired setup

Keep the customer's existing connection unchanged unless a new exchange succeeds. For cancellation, expiry or a rejected callback, offer a new Connect to Sage attempt. A redirect or a Linked row alone is not proof that your backend completed the exchange.

## Verify your integration

- Pair a disposable company, complete the callback, confirm the project connection and run `GET /info` followed by a resource read.

- Check that a wrong state, wrong verifier, reused code and expired session are rejected.

- Check that keys from another project cannot use the connection and that read-only grants cannot write.

- Handle `202` and poll the original operation. Test offline recovery, key revocation and connection disconnection.

- If using webhooks, verify signatures, duplicate handling and receiver failures.

Use the deployed [OpenAPI contract](https://sage-connector-public-api.nameless-union-7a77.workers.dev/openapi.json) for exact fields, response shapes and error codes.
