---
name: federal-access-api
description: Call the Federal Access API (`apex.federalaccess.cl/public/v1`) on behalf of one installation with an API key. Use when building or debugging an integration with Federal Access, handling its 401, 403 or 429 responses, or choosing an API key's scopes.
metadata:
  version: "1.0"
---

# Federal Access API

Federal Access is an access control platform. Its API lets a customer's systems (HR, ERP, building management) read and write one installation's data. Every request acts on the installation that owns the API key.

## Sources of truth

- Endpoints, parameters and schemas: the OpenAPI spec at `https://apex.federalaccess.cl/public/v1/openapi.yaml`. Read it before calling an endpoint; this skill doesn't list them.
- Guides: `https://docs.federalaccess.cl/en/guides/authentication`, `/en/guides/scopes`, `/en/guides/rate-limits`, `/en/guides/errors`. Spanish versions live under `/es/`.

## Requests

- Base URL: `https://apex.federalaccess.cl/public/v1`
- Auth header: `Authorization: Bearer <key>`. The key goes only in this header, never in the URL or body.
- Key format: `fa_<prefix>_<secret>`, with an 8-character prefix and a 40-character secret, both base58.
- Requests and responses are JSON.
- Keep the key server-side, in an environment variable or secrets manager. A user creates keys in the dashboard (**Configuración** > **API** > **Crear llave**); the full key is shown once.

Smoke test: `GET /installation` returns `{"data": {"id": <int>, "name": <string>}}`. Every key can call it.

## Scopes

Each endpoint requires one scope; its description in the spec names it. Scopes are fixed when the key is created: to change them, create a new key and revoke the old one.

`installation:read` (on every key), `accesses:read`, `members:read`, `members:write`, `invitations:read`, `invitations:write`. No scope writes the installation itself; its configuration only changes in the dashboard.

## Rate limit

- 5 requests per second **per installation**, shared by every key of that installation.
- The 6th request within one second answers `429` and **blocks the whole installation for 10 minutes**.
- During the block every request answers `429` with `Retry-After` set to the seconds left. These requests neither count nor extend the block.
- Counted: successful responses, missing-scope `403`s, unknown-path `404`s. Not counted: `401`s, inactive-installation `403`s.

Pace requests client-side below the limit (at most 4 per second through one queue per installation). On `429`, stop every request for that installation and sleep `Retry-After` seconds; exponential backoff only burns attempts against a fixed block. Only `429` sends `Retry-After`.

## Syncing accesses

`GET /accesses` is ordered by arrival to Federal Access (`received_at`), not by `occurred_at`. A device that was offline delivers old accesses late, and they appear after any stored cursor. To mirror the log:

1. Start without `cursor` (beginning of history), `limit` up to 1000 (default 500).
2. Store each page's accesses (upsert by `id`) and its `next_cursor` in one transaction.
3. While `has_more` is `true`, request again with `cursor=<next_cursor>`, paced under the rate limit.
4. When `has_more` is `false`, wait (for example a minute) and repeat with the same cursor, forever.

- `next_cursor` is always present and can advance on an empty page: always store the latest one. It's opaque; an invalid one is `400 invalid_parameter`.
- The cursor doesn't encode filters: send the same filters for the whole traversal, or start over without `cursor`.
- Date filters alone miss late arrivals. Build date reports from the synced copy by `occurred_at`, and recompute a day when a late access for it arrives. `occurred_from`/`occurred_to` are a half-open `[from, to)` range; values without a time zone are read in America/Santiago.
- The list only holds current accesses: a deleted one isn't announced, and `GET /accesses/{id}` answers `404` for it.

Guide: `https://docs.federalaccess.cl/en/guides/sync-accesses`.

## Errors

Every error uses one envelope: `{"error": {"code", "message", "request_id", "details"?}}`. Branch on `code`; `message` is for humans and may change. Log `request_id`; support needs it.

| Status | `code` | Cause | Action |
| --- | --- | --- | --- |
| 400 | `bad_request` | Body isn't valid JSON | Fix the body |
| 401 | `unauthorized` | Missing or malformed header, unknown or revoked key, or an installation that can't use the API | Check the key; every 401 is identical by design |
| 403 | `forbidden` | Key lacks the endpoint's scope (message names it), or the installation is inactive | Add the scope through a new key; an inactive installation needs Federal Access |
| 404 | `not_found` / `record_not_found` | Unknown path / record not in this installation | Fix the URL or id |
| 422 | `validation_error`, `business_rule_violation`, … | Invalid data or a broken rule | Read `message`, fix the data |
| 429 | `too_many_requests` | Rate limit; `details.retry_after` mirrors `Retry-After` | Wait, as above |
| 500 | `internal_error` | Unexpected | Retry later; report with `request_id` |

A `401` or `403` doesn't fix itself on retry, and a `403` or `404` still spends rate-limit budget.

## Checklist

- [ ] Base URL is `apex.federalaccess.cl`, path prefix `/public/v1`
- [ ] Each endpoint's scope is on the key
- [ ] One client-side queue per installation keeps requests under 5 per second
- [ ] `429` pauses every caller of that installation for `Retry-After` seconds
- [ ] Logic branches on `error.code`, and every error logs `request_id`
