Command Palette

Search for a command to run...

UnylyUnyly
Back to skills

adding-api-scopes

FreeNo executable scriptsNot checked

Guidance for adding an API scope object to the posthog/scopes/ package and making it work for personal API keys, OAuth tokens and MCP clients. Use when adding…

PostHogby PostHog★ 41,278 GitHub stars

About this skill

Adding an API scope

A scope has two parts: an object and an action, as in feature_flag:read. Adding a scope means adding a scope object, such as feature_flag. It gives tokens feature_flag:read and feature_flag:write. posthog/scopes/catalog.py is the source of the object list. The posthog/scopes/__init__.py docstring says what each module of the package holds. The frontend type and the MCP OAuth list are generated from it. Each object a picker offers also has a ScopeDoc: its name, plural, one-sentence description, group, docs link, the levels a picker offers, whether it reaches the whole organization, and any scope its writes also need. The owning product declares it in products/<name>/backend/scopes.py, and core objects are in CORE_SCOPE_DOCS in posthog/scope_docs.py. The web app's API_SCOPES and API_SCOPE_GROUPS are built from those declarations, so the frontend keeps no copy.

Decide if you need a new scope

Most endpoints fit an existing scope, for example insight:read. Add a new scope only for a product area that a person wants to grant or withhold on its own, on a key or an OAuth app. Name its object with a snake_case singular noun, such as feature_flag.

Decide what kind of scope it is

  • Public: OAuth lists it, MCP can request it, and the key picker offers it. This is the default.
  • Internal: only the server creates tokens with it. Use INTERNAL_API_SCOPE_OBJECTS.
  • OAuth-hidden: a person can paste it into a personal API key, but OAuth clients do not see it. Use this for staff-only or unreleased surfaces. Use OAUTH_HIDDEN_SCOPE_OBJECTS.
  • Privileged: only PostHog staff can give it to an OAuth app, through Django admin or a data migration. An app that registers itself cannot get it, and the CLI login and the key picker presets never include it. Use this for a scope that a partner app must not grant to itself, such as llm_gateway. Use PRIVILEGED_SCOPES. The picker row reads it from the generated scope docs.

Then decide two more things, separately from the kind:

  • Project secret API keys: a project secret API key is a project credential with no user, for server-to-server calls. Allow the scope on it only when such a caller needs it. The allowed list is PROJECT_SECRET_API_KEY_ALLOWED_API_SCOPE_ACTION, in both posthog/scopes/project_secret_api_key.py and frontend/src/lib/scopes.tsx. Read /adding-project-secret-api-key-auth first.
  • Access control: if an organization must be able to restrict the resource per role or per object, add it to ACCESS_CONTROL_RESOURCES in products/access_control/backend/facade/user_access_control.py. Access control resources use scope object names by design, so a viewset's scope_object names both its token scope and its access control resource. Do not give access control a naming or a type of its own. The resource fields of the access control serializers take GRANTABLE_API_SCOPE_OBJECTS as their choices, and the frontend APIScopeObject type is generated from those fields (be careful: the personal API key modal, the OAuth consent screen, CLI login and the key presets also use that type).

To add the scope

  1. Declare it in Python. Add the object to the APIScopeObject literal in posthog/scopes/catalog.py. If you chose internal, OAuth-hidden or privileged above, also add it to that set in posthog/scopes/exposure.py.
    • Add the same value to ResourceType in products/authorization/backend/facade/enums.py. The shared permission check names the scope with that type, so a scope without one returns a 500 on its endpoints.
  2. Connect it to its endpoints. Set scope_object = "<object>" on each viewset the scope covers.
    • Standard actions need no extra work: list and retrieve need :read, and create, update and destroy need :write.
    • A custom @action needs required_scopes, for example required_scopes=["<object>:write"]. Use :read if it only reads data, and :write if it changes data. Without it, token requests get a 403.
    • An endpoint set to scope_object = "INTERNAL" accepts only logged-in sessions. Change it to the new object to open it to tokens.
    • Then call each custom action with a personal API key that has only the new scope. No test checks this.
  3. Regenerate. Run hogli build:openapi, which carries the object to the frontend type, and hogli build:projections, which updates the OAuth lists of the MCP server and the web app. DO NOT EDIT GENERATED FILES BY HAND.
  4. Explain it. Skip this step for an OAuth-hidden object: no picker shows it, and the tests do not ask for a doc or a group.
    • Add a ScopeDoc to SCOPE_DOCS in products/<name>/backend/scopes.py, or to CORE_SCOPE_DOCS for a core object. Give it a sentence-case name, a plural that is lowercase except for proper nouns and acronyms, and one sentence under 120 characters on what the scope covers, without "read" or "write" (/writing-user-facing-copy). Link the
Full SKILL.md on GitHub

Install adding-api-scopes in Claude Code & Claude Desktop

Sign up to install this skill

Create a free account to reveal the install command and save the skill to your library.

  • Reveal the one-line install command
  • Save skills to your synced library
  • Get notified when skills update
Sign up freeI already have an account

Allowed tools

Tools this skill is permitted to call.

No restriction — this skill can use any tool.

FAQ

What does the adding-api-scopes skill do?

Guidance for adding an API scope object to the posthog/scopes/ package and making it work for personal API keys, OAuth tokens and MCP clients. Use when adding a scope object, exposing a viewset or MCP tool to tokens, moving a viewset off scope_object = "INTERNAL", deciding if a scope is internal, OAuth-hidden or privileged, choosing a scope group, renaming or splitting a scope object, reading the scope list in Python, the frontend or the MCP server, or when a scope test fails in scopes.test.ts, test_scopes.py or tool-filtering.test.ts. Trigger terms: new scope, APIScopeObject, ScopeObjectEnumApi, scope_object, required_scopes, API_SCOPES, API_SCOPE_GROUPS, mcp scopes, OAuth scopes, personal API key scope.

How do I install the adding-api-scopes skill?

Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.

Does the adding-api-scopes skill run scripts?

No, this skill is instructions only (SKILL.md) with no executable scripts.

Related skills

Compare adding-api-scopes with