|
| 1 | +# `clerk impersonate` |
| 2 | + |
| 3 | +Create a short-lived actor token for a Clerk user and print the sign-in URL that |
| 4 | +lets you sign in as them ("impersonate"). Alias: `clerk imp`. |
| 5 | + |
| 6 | +## Auth |
| 7 | + |
| 8 | +`clerk impersonate` and `clerk impersonate revoke` both **require `clerk auth |
| 9 | +login`** — there is no `--secret-key`-only bypass. The actor token's |
| 10 | +`actor.sub` field is stamped with your logged-in email (`cli:<email>`, or |
| 11 | +`cli:<email>+<context>` with `--actor <context>`) so every impersonation |
| 12 | +session is traceable back to a real Clerk account, not just an API key. |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +```sh |
| 17 | +clerk imp # pick a user interactively, then confirm |
| 18 | +clerk imp user_2x9k # impersonate a specific user |
| 19 | +clerk imp alice@example.com # resolve by exact email match |
| 20 | +clerk imp alice --open # open the sign-in URL in your browser immediately |
| 21 | +clerk imp user_2x9k --print # print the URL only, no prompt, no browser |
| 22 | +clerk imp user_2x9k --yes --expires-in 900 # skip confirmation, 15-minute token |
| 23 | +clerk imp user_2x9k --actor oncall # stamp the actor as cli:<you>+oncall |
| 24 | +clerk imp revoke act_29w9... # revoke a pending actor token |
| 25 | +``` |
| 26 | + |
| 27 | +## Options |
| 28 | + |
| 29 | +| Flag | Applies to | Description | |
| 30 | +| ------------------------ | ---------- | ---------------------------------------------------------------------------------------------------------------- | |
| 31 | +| `[user]` | create | `user_...` ID, exact email, or fuzzy search term. Omit to pick interactively. | |
| 32 | +| `<actorTokenId>` | revoke | Actor token ID to revoke (required) | |
| 33 | +| `--secret-key <key>` | both | Backend API secret key to use | |
| 34 | +| `--app <id>` | both | Application ID to target (works from any directory) | |
| 35 | +| `--instance <id>` | both | Instance to target (`dev`, `prod`, or a full instance ID) | |
| 36 | +| `--actor <context>` | create | Extra context appended to the actor stamp: `cli:<email>+<context>` | |
| 37 | +| `--expires-in <seconds>` | create | Actor token lifetime in seconds, integer >= 1. Defaults to 3600 (1 hour), matching the dashboard's short expiry. | |
| 38 | +| `--open` | create | Open the sign-in URL in your browser immediately, skipping the prompt | |
| 39 | +| `--print` | create | Print the sign-in URL only — no prompt, no browser | |
| 40 | +| `--yes` | create | Skip the confirmation prompt | |
| 41 | + |
| 42 | +`clerk impersonate revoke` never prompts for confirmation — it's a low-risk |
| 43 | +operation on an already-pending token. |
| 44 | + |
| 45 | +## User resolution |
| 46 | + |
| 47 | +Given `[user]`: |
| 48 | + |
| 49 | +1. `/^user_[A-Za-z0-9]+$/` → used directly, no lookup. |
| 50 | +2. Contains `@` → exact match via `GET /v1/users?email_address=<email>`. |
| 51 | +3. Otherwise → fuzzy match via `GET /v1/users?query=<term>`. |
| 52 | + |
| 53 | +Then: |
| 54 | + |
| 55 | +- 0 matches → usage error naming the searched app and instance, e.g. |
| 56 | + `No user found matching "alice@example.com" on My Application (development).` |
| 57 | +- 1 match → used directly. |
| 58 | +- 2+ matches, human mode → an interactive picker (the picker's search box |
| 59 | + always starts empty — there's no way to prefill it with your original |
| 60 | + search term; see `commands/users/interactive/pick-user.ts`). |
| 61 | +- 2+ matches, agent mode → usage error listing candidate user IDs so you can |
| 62 | + re-run with a specific `user_...` ID. |
| 63 | +- No `[user]` argument, human mode → the interactive picker. |
| 64 | +- No `[user]` argument, agent mode → usage error (agent mode never prompts). |
| 65 | + |
| 66 | +## Instance resolution |
| 67 | + |
| 68 | +The app comes from `--app`, the linked profile, or — in human mode with no |
| 69 | +linked project — an interactive app picker. In human mode, whenever the |
| 70 | +resolved app has more than one instance and no `--instance` flag was passed, |
| 71 | +`clerk impersonate` and `clerk impersonate revoke` prompt |
| 72 | +"Select an instance to use:" instead of silently defaulting to development — |
| 73 | +even in a linked project. Users usually exist on only one instance (and actor |
| 74 | +tokens are instance-scoped), so a silent development default makes lookups and |
| 75 | +revokes fail confusingly. Agent mode never prompts: when the app has more than |
| 76 | +one instance it errors and asks for an explicit `--instance` rather than |
| 77 | +defaulting, so the same command can't resolve a different instance depending on |
| 78 | +which instances the app happens to have. `--instance` or `--secret-key` always |
| 79 | +pins the instance. |
| 80 | + |
| 81 | +## Confirmation and the production guardrail |
| 82 | + |
| 83 | +In human mode, unless `--yes` is passed, `clerk impersonate` asks |
| 84 | +"Impersonate `<user>` on `<app>` (`<instance>`)?" defaulting to **No**. When |
| 85 | +the target instance's label is `production`, a warning is printed above the |
| 86 | +prompt: |
| 87 | + |
| 88 | +> production — signs you in as this user and bypasses their MFA. may count |
| 89 | +> against your monthly impersonation quota. |
| 90 | +
|
| 91 | +The CLI cannot read your live impersonation quota (Backend API has no |
| 92 | +endpoint for it), so this is a generic reminder, not a number. Agent mode |
| 93 | +never prompts and proceeds directly. |
| 94 | + |
| 95 | +## Output modes |
| 96 | + |
| 97 | +The sign-in URL from BAPI's response is always printed **verbatim** via |
| 98 | +`log.data()` — never reconstructed client-side. Human mode also prints a |
| 99 | +`Revoke with: clerk imp revoke <id>` hint to stderr — BAPI has no list |
| 100 | +endpoint for actor tokens, so creation is the only moment the ID is visible. |
| 101 | + |
| 102 | +| Condition | Behavior | |
| 103 | +| ------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 104 | +| `--print` | Print the URL, exit. No prompt, no browser. | |
| 105 | +| `--open` | Print the URL, open the browser immediately. No prompt. | |
| 106 | +| TTY, no `--print`/`--open` | Print the URL, then prompt "Press Enter to open in your browser (Ctrl+C to skip)". | |
| 107 | +| Non-TTY, human mode, no `--print`/`--open` | Same as `--print` — never hangs waiting for input that can't arrive. | |
| 108 | +| Agent mode | Emit one JSON line via `log.data()`: `{ url, id, userId, actor, appId, appLabel, instanceId, instanceLabel, expiresInSeconds }`. Never prompts, never opens a browser. | |
| 109 | + |
| 110 | +## Clerk API endpoints |
| 111 | + |
| 112 | +| Method | Path | Used by | |
| 113 | +| ------ | --------------------------------- | ---------------------------------------------------- | |
| 114 | +| `GET` | `/v1/users?email_address=<email>` | Resolving `[user]` when it contains `@` | |
| 115 | +| `GET` | `/v1/users?query=<term>` | Resolving `[user]` fuzzy search | |
| 116 | +| `GET` | `/v1/users?query=<term>&limit=21` | The interactive user picker (`pickUser`) | |
| 117 | +| `POST` | `/v1/actor_tokens` | Creating the actor token (`clerk impersonate`) | |
| 118 | +| `POST` | `/v1/actor_tokens/{id}/revoke` | Revoking an actor token (`clerk impersonate revoke`) | |
| 119 | + |
| 120 | +`POST /v1/actor_tokens` returns `402` when impersonation isn't enabled on the |
| 121 | +app's subscription plan, and `422` when the impersonation quota is exhausted |
| 122 | +(the CLI surfaces `used`/`limit` from the error's `meta` when BAPI includes |
| 123 | +them). |
| 124 | + |
| 125 | +No part of this command is mocked or stubbed. |
0 commit comments