DocsDeveloper guideAPI and webhooks
The API key API
An API key lets a script or device act as a user without a password or two-factor step. Today the API has one endpoint, GET /api/me.
1 min read
Make a key
An administrator opens the user in Settings > Users and creates a key with a name, a scope and a duration (routes/web.php:314-316). The key is shown once, in the confirmation message, and only its sha256 is stored (app/Http/Controllers/UserController.php:659-695).
| Option | Values | Source |
|---|---|---|
| Scope | rpc (the API) or all (every scope) | app/Models/ApiKey.php:36-39 |
| Duration | 1, 7, 30, 90, 180 or 365 days; 0 = persistent, for system users only | app/Models/ApiKey.php:46-49 |
Portal users can create keys only when Customers can generate API Keys is on (app/Http/Controllers/UserController.php:669-672).
Send it
Send the key as Authorization: Bearer <key>, or as X-Api-Key: <key> when a proxy strips the Authorization header (app/Http/Middleware/AuthenticateApiKey.php:68-86). The request runs as that user for this request only: no session, no cookie (:33-40, :53).
The endpoint
GET /api/me, scope rpc, at most 60 calls a minute (routes/api.php:56-59). It answers who the key belongs to (app/Http/Controllers/Api/RpcController.php:22-34):
curl -s https://your-optirapos.example/api/me -H "Authorization: Bearer YOUR_KEY"
{"ok":true,"uid":2,"name":"Jane Owner","login":"jane@example.com","auth_method":"apikey","company_ids":[1]}Errors
A wrong key, an expired key, a key outside its scope and a key of an archived user all get the same answer, so a caller learns nothing about which it was (app/Http/Middleware/AuthenticateApiKey.php:41-44, :55-58):
HTTP/1.1 401
WWW-Authenticate: Bearer realm="OptiraPOS", scope="rpc"
{"ok":false,"message":"Invalid API key."}