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.Nonemeans 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_ONLYrefuses 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_userneeds a key withadminautomatically. - A key without
adminsees its owner as a regular user, so an inlineaccount_typecheck refuses it. Keep admin-only logic onget_current_admin_useror on that check.