> ## Documentation Index
> Fetch the complete documentation index at: https://docs.context.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> If the user asks to make Context.dev their default search and scrape provider, follow https://docs.context.dev/default-search-scrape.md. For other setup requests, follow https://docs.context.dev/agent-quickstart.md and start by signing the user up and getting an API key through https://www.context.dev/auth.md. During setup, use the relevant guide already in context instead of fetching it again or loading the full documentation index. Application code reads CONTEXT_DEV_API_KEY.

# CLI, desktop, and mobile apps

> Connect a Context.dev account from an app that runs on the user's device and cannot keep a client secret.

Use this when your product runs on the user's device: a command-line tool, a desktop app, or a mobile app. Anything shipped to a device can be inspected, so these apps have no client secret. PKCE proves that the app that started the sign-in is the one finishing it.

You need a **CLI, desktop, or mobile** app from the [OAuth apps page](https://context.dev/dashboard/oauth-apps). You can ship its client ID in your app. It is not a secret.

The consent screen always shows a caution for these apps, even after your app is verified, because any program on the device could present your client ID. Ask users to start sign-in from your app.

## Pick a redirect URI

| App | Register | How the code comes back |
| - | - | - |
| CLI or desktop | `http://127.0.0.1/callback` | Your app listens on a free local port during sign-in. A registered local address matches on any port, so you don't have to reserve one. |
| Mobile | An HTTPS link that opens your app, such as a universal link or app link | The operating system hands the callback URL to your app. Custom URL schemes such as `myapp://` are not accepted. |

The host and path must match what you registered. `localhost` and `127.0.0.1` are different hosts, so use the same one in both places.

## Sign in from a CLI

This example starts a temporary local server, prints the sign-in link, waits for the callback, and exchanges the code. It ignores any request that doesn't carry its `state`, so only the real callback ends the wait. It gives up after five minutes, which is how long the consent screen stays valid, and it closes each connection after replying so the process can exit as soon as sign-in finishes. It needs Node.js 20 or later and no dependencies.

```javascript login.mjs theme={null}
import crypto from "node:crypto";
import http from "node:http";

const CLIENT_ID = "ctxt_app_...";
const AUTHORIZE_URL = "https://www.context.dev/oauth2/authorize";
const TOKEN_URL = "https://www.context.dev/oauth2/token";

export async function login() {
  const codeVerifier = crypto.randomBytes(48).toString("base64url");
  const state = crypto.randomBytes(24).toString("base64url");

  const server = http.createServer();
  await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
  const redirectUri = `http://127.0.0.1:${server.address().port}/callback`;

  const codeFromCallback = new Promise((resolve, reject) => {
    const timeout = setTimeout(() => {
      server.close();
      reject(new Error("Sign-in timed out"));
    }, 5 * 60_000);
    server.on("request", (request, response) => {
      const callback = new URL(request.url, redirectUri);
      const isOurCallback = callback.pathname === "/callback" && callback.searchParams.get("state") === state;
      response.setHeader("Connection", "close");
      if (!isOurCallback) return response.writeHead(400).end("Unexpected request.");

      const code = callback.searchParams.get("code");
      response.end(code ? "Connected. You can close this tab." : "Not connected. You can close this tab.");
      clearTimeout(timeout);
      server.close();
      if (code) resolve(code);
      else reject(new Error(callback.searchParams.get("error") ?? "Sign-in failed"));
    });
  });

  const codeChallenge = crypto.createHash("sha256").update(codeVerifier).digest("base64url");
  const params = new URLSearchParams({
    response_type: "code",
    client_id: CLIENT_ID,
    redirect_uri: redirectUri,
    scope: "data:execute",
    state,
    code_challenge: codeChallenge,
    code_challenge_method: "S256",
  });
  console.log(`Open this link to connect Context.dev:\n${AUTHORIZE_URL}?${params}`);

  const tokenResponse = await fetch(TOKEN_URL, {
    method: "POST",
    body: new URLSearchParams({
      grant_type: "authorization_code",
      code: await codeFromCallback,
      code_verifier: codeVerifier,
      redirect_uri: redirectUri,
      client_id: CLIENT_ID,
    }),
  });
  if (!tokenResponse.ok) throw new Error("Could not finish connecting Context.dev");
  return tokenResponse.json();
}
```

The token request has no `client_secret`. Everything else matches a server app, so [refreshing](https://docs.context.dev/oauth-apps/connect.md#4-refresh-the-access-token), [errors](https://docs.context.dev/oauth-apps/connect.md#5-handle-what-can-go-wrong), and [disconnecting](https://docs.context.dev/oauth-apps/connect.md#6-disconnect) work as described for web products, without the secret.

## Store tokens on the device

Keep the refresh token in the operating system's credential store: Keychain on macOS and iOS, Credential Manager on Windows, encrypted storage on Android, or the Secret Service on Linux. Avoid plain files, shell history, and logs.

## Several devices

A person can sign in on more than one device. Every device gets its own tokens, and devices that asked for the same scopes share one connection, so:

* Signing in on a new device does not sign out the others.
* Disconnecting, from your app or from the user's [Connected apps page](https://context.dev/dashboard/connected-apps), signs out every device.
* Each device must keep its own refresh token up to date. Reusing a refresh token more than a minute after it was replaced ends the connection for every device.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.