PotionUI Docs v0.3.0

API keys on plugin routes

A script can call the app with an API key instead of a browser session. A key carries scopes (read, generate, content, admin, mcp), and a route accepts a key only if it says which scope it needs. A plugin route that declares nothing refuses every key with 403 key_not_allowed; browser sessions are unaffected. Declare what your routes need from src.plugin_api.identity:

python
from src.plugin_api.identity import ApiScope, SESSION_ONLY, key_access, key_scope

router = APIRouter(
    prefix="/api/plugins/my-plugin",
    dependencies=[key_access(read=ApiScope.READ, write=ApiScope.CONTENT)],
)

@router.post("/render", dependencies=[key_scope(ApiScope.GENERATE)])
async def render(...): ...

@router.post("/connect", dependencies=[SESSION_ONLY])
async def connect(...): ...
  • key_access(read=..., write=...) on the router sets the scope for GET and HEAD, and for every other method. None means keys cannot use that half.
  • key_scope(...) on one route overrides the router for every method. Give several scopes to accept any of them.
  • SESSION_ONLY refuses keys on that route whatever else is declared. Use it for anything that manages credentials or keys.
  • A route that depends on get_current_admin_user needs a key with admin automatically.
  • A key without admin sees its owner as a regular user, so an inline account_type check refuses it. Keep admin-only logic on get_current_admin_user or on that check.