Chapter 06
App API (REST)
How the mobile app, the operator console and your own systems talk to the Nook backend: sign-in, the routes to list and open compartments, assignments, webhooks, and the error codes every client must handle.
Draft specification, version 1. This is the contract between the mobile app, the operator console and the Nook backend. It is written to be implemented: nothing here exists in production yet. The backend translates these calls into the signed MQTT commands described in the Cabinet API.
Overview
App or console ──HTTPS──▶ Nook backend ──MQTT/TLS──▶ Cabinet
◀── JSON ── ◀── events ──- The app never holds a cabinet secret. It only holds a short-lived session token.
- The backend decides who may open which compartment, signs the command and waits for the cabinet's answer.
- The same API serves three kinds of callers: members (the mobile app), operators (the venue's console) and integrations (your booking system, shop or CRM, with an API key).
Conventions
| Topic | Rule |
|---|---|
| Base URL | https://api.nooklocker.es/v1. HTTPS only, TLS 1.2 or newer. |
| Format | JSON in and out, UTF-8, Content-Type: application/json. Field names in camelCase. |
| Dates | ISO 8601 in UTC, for example 2026-11-24T18:42:07Z. |
| Identifiers | Opaque strings with a prefix: mem_… member, cab_… cabinet, asg_… assignment, unl_… unlock, evt_… event. Compartments are numbered from 1. |
| Idempotency | Every POST that changes something accepts an Idempotency-Key header (a UUID). The same key within 24 h returns the first response instead of acting twice. |
| Pagination | Cursor based: ?limit=50&cursor=…. Responses carry nextCursor, null on the last page. limit is 1 to 100, 50 by default. |
| Versioning | Breaking changes get a new prefix (/v2). Adding fields is not a breaking change: clients must ignore unknown fields. |
| Request id | Every response carries X-Request-Id. Quote it when you contact support. |
Authentication
Members and operators sign in with a one-time code sent by email or SMS. No password is stored. The backend returns a short access token and a long refresh token.
| Caller | Credential | Header |
|---|---|---|
| Member, operator | Access token (JWT, 15 min) and refresh token (opaque, 30 days, rotated on every use) | Authorization: Bearer <accessToken> |
| Integration | API key created in the console, scoped to one organisation, revocable | Authorization: Bearer nk_live_… |
POST /auth/code: send a sign-in code
POST /v1/auth/code
Content-Type: application/json
{ "email": "lea@example.com" }{ "challengeId": "chl_8f3a1c", "channel": "email", "expiresAt": "2026-11-24T18:52:07Z" }Use phone instead of email for SMS. The answer is the same whether or not the address is known, so the endpoint cannot be used to find members. Limited to 5 requests per address per hour.
POST /auth/verify: exchange the code for tokens
{ "challengeId": "chl_8f3a1c", "code": "482915" }{
"accessToken": "eyJhbGciOiJFZERTQSIs…",
"accessTokenExpiresAt": "2026-11-24T19:07:30Z",
"refreshToken": "rt_2b9f…",
"member": { "id": "mem_42", "name": "Léa Martin", "role": "member" }
}A code is 6 digits, valid 10 minutes, and blocked after 5 wrong tries.
POST /auth/refresh and POST /auth/logout
{ "refreshToken": "rt_2b9f…" }/auth/refresh returns a new pair of tokens and invalidates the old refresh token. If an old refresh token is used again, the whole session is revoked: it was probably stolen. /auth/logout revokes the refresh token and answers 204.
Member endpoints (mobile app)
GET /me
{
"id": "mem_42", "name": "Léa Martin", "email": "lea@example.com",
"role": "member", "organisation": { "id": "org_7", "name": "Hôtel des Arts" }
}GET /lockers: the compartments assigned to me
{
"data": [
{
"assignmentId": "asg_311",
"cabinet": { "id": "cab_001", "name": "Lobby", "model": "nook-locker-100", "online": true },
"locker": 17,
"door": "closed",
"validFrom": "2026-11-24T15:00:00Z",
"validUntil": "2026-11-27T11:00:00Z",
"canOpen": true
}
],
"nextCursor": null
}door is open, closed or unknown (cabinet offline or lock board not answering). canOpen is false outside the validity window.
POST /lockers/{assignmentId}/open: open my compartment
The app opens an assignment, not a raw compartment number, so it can never ask for a compartment it was not given.
POST /v1/lockers/asg_311/open
Authorization: Bearer eyJhbGciOiJFZERTQSIs…
Idempotency-Key: 4b7d6a1e-9c3f-4f0e-8d55-2a71c0e9b310The backend signs the command, publishes it and waits up to 8 seconds for the cabinet's event/unlock.
{
"id": "unl_9a0c",
"status": "opened",
"locker": 17,
"cabinetId": "cab_001",
"doorOpen": true,
"requestedAt": "2026-11-24T18:42:07Z",
"completedAt": "2026-11-24T18:42:08Z"
}status is one of:
opened: the lock released and the door moved.released: the lock released but the door did not move (jammed or held shut). The app should say "Push the door".pending: no answer within 8 s; the response is202 Accepted. PollGET /unlocks/{id}every second for up to 20 s.
Refusals and cabinet problems come back as errors, see Errors.
GET /unlocks/{id}
Returns the same object as above. Final statuses are opened, released and failed (with an error object); pending means the cabinet has not answered yet.
POST /lockers/{assignmentId}/offline-tokens: Bluetooth tokens for offline use
{ "count": 3 }{
"data": [
{ "token": "v1.eyJjYWIiOiJjYWItMDAx…", "nonce": "9f2c4e7a", "validFrom": "2026-11-24T15:00:00Z", "validUntil": "2026-11-27T11:00:00Z" }
],
"ble": { "name": "Nook-cab-001", "service": "6e6f6f6b-0001-4c6f-636b-657200000000" }
}The app stores the tokens in the device's secure storage (Keychain, Keystore) and uses one only when the online open fails with cabinet_offline. A token never outlives the assignment, and at most 7 days. count is 1 to 5; the app tops up as tokens are used.
GET /activity: my history
{
"data": [
{ "id": "evt_5512", "type": "unlock.succeeded", "cabinetId": "cab_001", "locker": 17, "source": "app", "at": "2026-11-24T18:42:08Z" },
{ "id": "evt_5513", "type": "door.closed", "cabinetId": "cab_001", "locker": 17, "at": "2026-11-24T18:42:31Z" }
],
"nextCursor": "c_evt_5511"
}Operator endpoints (console and integrations)
Reserved to the operator role and to API keys. Every call is limited to the caller's organisation.
| Method and path | Purpose |
|---|---|
GET /cabinets | List cabinets with online, power state and free compartments |
GET /cabinets/{id} | Full status: last heartbeat, boards, battery, open doors, pending events |
GET /cabinets/{id}/doors | State of every door, from the last doors message |
POST /cabinets/{id}/sync | Ask the cabinet for a fresh state (sends cmd/sync). 202. |
POST /cabinets/{id}/lockers/{n}/open | Open any compartment, for staff. Requires a reason in the body; it is logged. |
GET /members, POST /members | List or invite members (name and email or phone). The invite sends a sign-in link. |
DELETE /members/{id} | Remove a member. Their assignments end at once and their offline tokens stop being issued. |
GET /assignments, POST /assignments | List or create an assignment: who gets which compartment, and when |
PATCH /assignments/{id} | Change the dates or move to another compartment |
DELETE /assignments/{id} | End an assignment now |
GET /events | Audit log, filterable by cabinetId, locker, memberId, type, from, to |
GET /webhooks, POST /webhooks, DELETE /webhooks/{id} | Manage webhook endpoints |
POST /assignments
{
"memberId": "mem_42",
"cabinetId": "cab_001",
"locker": 17,
"validFrom": "2026-11-24T15:00:00Z",
"validUntil": "2026-11-27T11:00:00Z",
"notify": true
}{ "id": "asg_311", "memberId": "mem_42", "cabinetId": "cab_001", "locker": 17,
"validFrom": "2026-11-24T15:00:00Z", "validUntil": "2026-11-27T11:00:00Z", "status": "scheduled" }Omit locker to let the backend pick a free compartment. validUntil can be null for an open-ended assignment. A compartment cannot have two overlapping assignments: that returns 409 locker_taken. With notify, the member receives a push notification or an email.
Webhooks
Integrations can receive events instead of polling. The backend sends a POST with a JSON body to each registered URL and expects a 2xx within 5 seconds. Failed deliveries are retried with backoff for 24 hours.
| Event type | When |
|---|---|
unlock.succeeded, unlock.failed | After every unlock attempt, online or Bluetooth |
door.opened, door.closed | Door movements |
door.forced, door.left_open | Security alerts |
cabinet.offline, cabinet.online | Connection lost (Last Will) or restored |
cabinet.on_battery, cabinet.power_restored, cabinet.low_battery | Power events |
cabinet.fault | A lock board stopped or started answering |
assignment.started, assignment.ended | Start and end of an assignment window |
POST https://your-system.example.com/nook
Content-Type: application/json
Nook-Event-Id: evt_5512
Nook-Signature: t=1764009728,v1=5257a869e7ecebeda32affa62cdca3fa51cad7e77a0e56ff536d0ce8e108d8bd
{ "id": "evt_5512", "type": "unlock.succeeded", "at": "2026-11-24T18:42:08Z",
"data": { "cabinetId": "cab_001", "locker": 17, "memberId": "mem_42", "source": "app", "doorOpen": true } }Check the signature before trusting the body: v1 = HMAC-SHA256(webhookSecret, "{t}.{raw body}"), and refuse deliveries older than 5 minutes. Use Nook-Event-Id to ignore duplicates.
import { createHmac, timingSafeEqual } from 'node:crypto';
export function verify(rawBody, header, secret) {
const { t, v1 } = Object.fromEntries(header.split(',').map((kv) => kv.split('=')));
if (Math.abs(Date.now() / 1000 - Number(t)) > 300) return false;
const expected = createHmac('sha256', secret).update(`${t}.${rawBody}`).digest('hex');
return timingSafeEqual(Buffer.from(expected), Buffer.from(v1));
}Errors
Every error has the same shape. code is stable and meant for programs; message is for people and may change.
{
"error": {
"code": "cabinet_offline",
"message": "The cabinet is not connected. Use Bluetooth to open your locker.",
"status": 503,
"requestId": "req_7c1e9b",
"details": { "cabinetId": "cab_001", "lastSeenAt": "2026-11-24T18:31:55Z" }
}
}| HTTP | Code | Meaning |
|---|---|---|
| 400 | invalid_request | Malformed JSON or a missing field. details.fields lists them. |
| 401 | unauthenticated | Missing or invalid token |
| 401 | token_expired | Access token expired: call /auth/refresh and retry |
| 401 | invalid_code | Wrong or expired sign-in code. details.attemptsLeft says how many tries remain. |
| 403 | forbidden | The caller's role does not allow this action |
| 403 | assignment_not_active | Outside the assignment's validity window. details.validFrom, details.validUntil. |
| 404 | not_found | Unknown resource, or not in the caller's organisation |
| 409 | locker_taken | The compartment already has an assignment for that period |
| 409 | idempotency_conflict | Same Idempotency-Key with a different body |
| 422 | validation_failed | Well-formed but invalid values, such as validUntil before validFrom |
| 429 | rate_limited | Too many requests. Wait for the number of seconds in Retry-After. |
| 502 | cabinet_refused | The cabinet refused the command. details.reason carries its reason, for example expired or too many requests. |
| 502 | lock_board_unreachable | The cabinet answered but the lock board did not (board N did not answer) |
| 503 | cabinet_offline | No heartbeat for more than 90 s. The app should offer Bluetooth. |
| 504 | cabinet_timeout | The command was sent but no answer came within 20 s. The door may have opened: check GET /unlocks/{id} later. |
| 500 | internal_error | Unexpected server error. Safe to retry with the same Idempotency-Key. |
Rate limits
| Scope | Limit |
|---|---|
| Sign-in codes | 5 per address per hour, 5 wrong tries per code |
| Open a compartment | 10 per member per minute. The cabinet adds its own limit of one unlock per compartment every 2 s. |
| Other member calls | 120 per member per minute |
| Integrations | 600 per API key per minute |
Responses carry RateLimit-Limit, RateLimit-Remaining and RateLimit-Reset.
What the backend does on an open
- Authenticate the caller and load the assignment.
- Check that the assignment belongs to the caller and is active, and that the cabinet sent a heartbeat in the last 90 s.
- Store an
unlockrow with statuspendingand a newrequestId. - Sign the command with the cabinet's secret (
sign_commandinnook/security.py), withexp30 s ahead, and publish it oncabinet/{id}/cmd/openwith QoS 1. - Match the incoming
event/unlockbyrequestId, update the row, answer the app and send the webhooks. - Write every step to the audit log, including refusals.