API Tokens JavaScript API
Everything the API Tokens extension adds to pb_hooks and page code, generated from the TypeScript declarations it ships (your app's types.d.ts has the same, so your editor knows them). For how to use it, see its docs; for Go, the Go API.
$apiTokens
API token scopes.
$apiTokens.registerScope({ name: "notes:write", description: "Change notes", paths: ["/api/collections/notes/records"] })
routerAdd("POST", "/api/notes/archive", (e) => { ... }, $apis.requireAuth(), $apiTokens.requireScope("notes:write"))
$apiTokens.registerScope()
$apiTokens.registerScope(def: tokens.ScopeDef): void
Registers a scope (top level of a pb_hooks file); the same name again merges.
| Parameter | Type | |
|---|---|---|
def |
tokens.ScopeDef |
$apiTokens.requireScope()
$apiTokens.requireScope(name: string): any
Route middleware: a token, if the request uses one, must hold the scope.
| Parameter | Type | |
|---|---|---|
name |
string |
Returns any.
$apiTokens.hasScope()
$apiTokens.hasScope(e: core.RequestEvent, name: string, value?: string): boolean
Whether the request may use the scope (always true without a token). With a value: whether it may use the scope on that value (it holds the bare scope, or "name=value").
| Parameter | Type | |
|---|---|---|
e |
core.RequestEvent |
|
name |
string |
|
value |
string (optional) |
Returns boolean.
$apiTokens.scopeValues()
$apiTokens.scopeValues(e: core.RequestEvent, name: string): { all: boolean; values: string[] }
The values the request holds the scope for: all is true for every value.
| Parameter | Type | |
|---|---|---|
e |
core.RequestEvent |
|
name |
string |
Returns { all: boolean; values: string[] }.
$apiTokens.ruleField()
$apiTokens.ruleField(name: string): string
The collection-rule field for the scope.
| Parameter | Type | |
|---|---|---|
name |
string |
Returns string.
$apiTokens.scopes()
$apiTokens.scopes(): tokens.Scope[]
Every registered scope.
Returns tokens.Scope[].
Types
tokens.ScopeDef
A scope API tokens and OAuth clients can be granted.
| Member | Type | |
|---|---|---|
name |
string |
e.g. "notes:read". |
description? |
string |
|
paths? |
string[] |
Path prefixes the scope covers. |
methods? |
string[] |
HTTP methods it covers (all when empty). |
collections? |
string[] |
Collections whose record APIs it covers. |
values? |
{ description: string } |
Lets a token hold the scope for some values only, written "name=value" (e.g. "notes=abc123"); the bare name still means every value. Routes check the value with hasScope(e, name, value). |
tokens.Scope
| Member | Type | |
|---|---|---|
header |
string |
The request header a token's scope shows up in. |
ruleField |
string |
The field to use in collection rules, e.g. "@request.headers.x_token_scope_notes_read". |
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.