# Connect with Groundbox

If your product runs your users' CI jobs or sandboxes on Groundbox, add a **Connect with Groundbox** button instead of asking them to make and paste an API key. Your user signs in to Groundbox, or creates an account on the way, picks the team, and approves. Your app gets an ordinary Groundbox API key for that team, with the scopes you asked for.

It is OAuth 2.1's authorization code flow with PKCE, the same one Groundbox's MCP server uses, so any OAuth library works.

## Get a client ID

Write to us through [support](https://groundbox.dev/support) with your app's name, its redirect URIs, a square logo (https) and its website. We register it as a verified partner app and send you its client ID (`gbc_...`). Your users see your name and logo on the consent page marked as verified, and the accounts created through your button are counted as yours.

Redirect URIs must be https, http on localhost, or an app's own scheme, with no fragment, and must match exactly (any port on localhost). To try the flow before then, register a client yourself with `POST /oauth/register` ([dynamic registration](https://groundbox.dev/docs/mcp)); the consent page shows its name as given, not verified.

## The flow

1. **Send your user's browser to the authorize URL.** Make a fresh `code_verifier` (43 to 128 characters) and a `state` for each connection and keep both with the user's session. `https://api.groundbox.dev/oauth/authorize ?response_type=code &client_id=gbc_... &redirect_uri=https://yourapp.example/connect/groundbox/callback &scope=ci:read ci:write account:read billing:read &state=<random, tied to your user's session> &code_challenge=<base64url(sha256(code_verifier))> &code_challenge_method=S256`
2. **Your user signs in or signs up, picks a team and approves.** Someone without an account can create one on the way, with $5 of free usage and no card. Add `prompt=create` to open on sign-up when you know the person is new.
3. **Groundbox sends the browser back** to your redirect URI with `code` and your `state`, or with `error=access_denied` if they said no. Check the state.
4. **Trade the code for the key** within two minutes. A code works once, and a wrong verifier burns it. `curl -X POST https://api.groundbox.dev/oauth/token \ -d grant_type=authorization_code \ -d code=gbo_... \ -d redirect_uri=https://yourapp.example/connect/groundbox/callback \ -d client_id=gbc_... \ -d code_verifier=<the verifier you made for this connection>` `{ "access_token": "gbx_...", "token_type": "Bearer", "scope": "ci:read ci:write account:read billing:read" }`

The same in Node:

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

// When your user clicks "Connect with Groundbox":
const verifier = randomBytes(32).toString("base64url");
const challenge = createHash("sha256").update(verifier).digest("base64url");
const state = randomBytes(16).toString("base64url");
// Keep verifier and state with the user's session, then redirect:
const url = new URL("https://api.groundbox.dev/oauth/authorize");
url.search = new URLSearchParams({
  response_type: "code",
  client_id: process.env.GROUNDBOX_CLIENT_ID,
  redirect_uri: "https://yourapp.example/connect/groundbox/callback",
  scope: "ci:read ci:write account:read billing:read",
  state,
  code_challenge: challenge,
  code_challenge_method: "S256",
}).toString();

// On the callback: check state, then trade the code for the key.
const response = await fetch("https://api.groundbox.dev/oauth/token", {
  method: "POST",
  headers: { "content-type": "application/x-www-form-urlencoded" },
  body: new URLSearchParams({
    grant_type: "authorization_code",
    code: callbackParams.get("code"),
    redirect_uri: "https://yourapp.example/connect/groundbox/callback",
    client_id: process.env.GROUNDBOX_CLIENT_ID,
    code_verifier: verifier,
  }),
});
const { access_token } = await response.json(); // gbx_..., store it encrypted
```

## Scopes

Ask for what your app needs, space-separated in `scope`; your user sees each one in plain words before approving. Leave it out and you get the MCP set: sandboxes, schedules, storage, `ci:read` and `account:read`. The full list is under [Keys and caps](https://groundbox.dev/docs/api#keys). A CI runner provider usually wants `ci:read ci:write account:read billing:read`.

## Usage and limits

With `account:read`, `GET /v1/account` answers the team's balance, its usage this month and its limits. With `billing:read`, `GET /v1/billing` answers the plan and billing settings, and `GET /v1/billing/usage` the usage by day.

```
curl https://api.groundbox.dev/v1/account -H "Authorization: Bearer gbx_..."
curl https://api.groundbox.dev/v1/billing -H "Authorization: Bearer gbx_..."
```

## The key

- It does not expire, so there is no refresh token. Store it as you would a password. It stops working when your user revokes it on the dashboard's [API keys](https://app.groundbox.dev/keys) page, where it is listed under your app's name and can be given a spend cap.
- When your user disconnects Groundbox in your app, revoke the key: `curl -X POST https://api.groundbox.dev/oauth/revoke -d token=gbx_...`
- It spends from the team your user picked. Each connection makes a new key; connecting again does not end the old one, so revoke it.

Questions: [support](https://groundbox.dev/support).
