# API reference

Every route of the project API. The address is `https://api.rezincore.com`.

## Conventions

- **Key.** Every request carries a project key in the `apikey` header: `rzc_pub_…` (publishable) or `rzc_sec_…` (secret).
- **User.** A signed-in user's token goes in `Authorization: Bearer <token>`.
- **Server.** With a secret key and no token, name the organization in `x-organization-id`.
- **Bodies** are JSON with `content-type: application/json`, except file uploads.
- **Errors** are JSON: `{ "error": "<code>", "message": "<words>" }`. See [Errors](https://rezincore.com/docs/reference/errors.md).
- **Sizes**: 8 KB for sign-in and sign-up bodies, 256 KB for data, 25 MB for a file.
- **Rates**: 600 requests a minute per key, 300 a minute per network address. See [Limits](https://rezincore.com/docs/reference/limits.md).
- Answers are never cached (`Cache-Control: no-store`).

In the tables below, **Needs** says what the request must carry: *key* (any project key), *user* (a key and a user's token), *caller* (a key and either a user's token or, with a secret key, `x-organization-id`), or *nothing*.

## Service

| Route | Needs | Answer |
|---|---|---|
| `GET /health` | nothing | `{ "ok": true }` |
| `GET /v1` | key | `{ "project": "<name>", "key": "publishable" or "secret", "signup": true or false }` |

## Accounts

Guides: [Accounts for your users](https://rezincore.com/docs/auth/overview.md), [Email flows](https://rezincore.com/docs/auth/email-flows.md), [Google and GitHub](https://rezincore.com/docs/auth/providers.md).

### POST /v1/auth/signup

Needs: key. Body: `email`, `password` (12 to 200 characters), `fullName` (optional), `redirectTo` (optional).

- `201 { "id": "<user id>" }`
- `202 { "confirmationRequired": true }` when the project confirms addresses

Errors: `400 bad_request`, `400 weak_password`, `403 signup_closed`, `403 method_off`, `409 email_in_use`, `409 site_url_required`, `503 email_not_ready`, `429 too_many_requests`.

### POST /v1/auth/signin

Needs: key. Body: `email`, `password`, `code` (optional: authenticator or recovery code).

```json
{ "token": "…", "userId": "…", "organizationId": "…", "mustChangePassword": false, "idleMinutes": 15 }
```

Errors: `401 bad_login`, `401 code_required`, `403 method_off`, `429 too_many_requests`.

### GET /v1/auth/user

Needs: user.

```json
{ "id": "…", "email": "sam@example.test", "fullName": "Sam Rivera", "organization": { "id": "…", "name": "Example Co" }, "mustChangePassword": false }
```

Errors: `401 signed_out`.

### POST /v1/auth/signout

Needs: user. Answer: `{ "ok": true }`.

### GET /v1/auth/authorize/:provider

Needs: nothing; opened in the person's browser. `:provider` is `google` or `github`. Query: `project` (the project ID), `redirect_to` (optional; a listed address).

Answer: `302` to the provider.

Errors: `404 not_found`, `403 method_off`, `400 redirect_not_allowed`, `503 not_configured`, `429 too_many_requests`.

### GET /v1/auth/callback/:provider

Where the provider sends the person back. You never call it. It answers `302` to your `redirect_to` with `?code=<one-time code>` or `?error=<reason>`: `cancelled`, `not_verified`, `no_account`, `disabled`, `not_configured`. A link that has expired or was already used answers `400 bad_state`.

### POST /v1/auth/exchange

Needs: key. Body: `code` (the one-time code), `secondStepCode` (optional).

Answer: the same shape as sign-in.

Errors: `401 bad_code`, `401 code_required`, `401 bad_second_step`, `429 too_many_requests`.

### POST /v1/auth/magiclink

Needs: key. Body: `email`, `redirectTo` (optional). Answer: `202 { "sent": true }`, whether or not the address has an account.

Errors: `403 method_off`, `400 bad_redirect`, `503 email_not_ready`, `429 too_many_requests`.

### POST /v1/auth/verify

Needs: key. Body: `type` and what proves it.

| `type` | Send | Answer |
|---|---|---|
| `signup` | `token` | `{ "confirmed": true, "id": "<user id>" }` |
| `magiclink` | `token`, or `email` and `code`; `secondStepCode` if asked | The same shape as sign-in |
| `email_change` | `token` | `{ "changed": true }` |
| `recovery` | `token`, `password` | `{ "ok": true, "email": "…" }` |
| `invite` | `token`, `password` | `{ "ok": true, "email": "…" }` |

Errors: `401 bad_code`, `401 code_required`, `401 bad_second_step`, `403 method_off`, `409 email_in_use`, `400 weak_password`.

### POST /v1/auth/recover

Needs: key. Body: `email`, `redirectTo` (optional). Answer: `202 { "sent": true }`, whether or not the address has an account. See [Changing and resetting a password](https://rezincore.com/docs/auth/overview.md#changing-and-resetting-a-password).

Errors: `403 method_off`, `400 bad_redirect`, `400 site_url_needed`, `503 email_not_ready`, `429 too_many_requests`.

### POST /v1/auth/password

Needs: key and the account's token. Body: `currentPassword`, `newPassword`. Answer: `{ "ok": true }`.

Errors: `401 bad_password`, `400 weak_password`, `401 signed_out`.

### The authenticator step

Needs: key and the account's token. `GET /v1/auth/second-step`, `POST /v1/auth/second-step/start`, `POST /v1/auth/second-step/confirm` (`code`), `POST /v1/auth/second-step/off` (`password`). See [Setting it up through the API](https://rezincore.com/docs/auth/authenticator.md#setting-it-up-through-the-api).

Errors: `409 already_on`, `400 bad_code`, `401 bad_password`, `403 password_change_required`.

### POST /v1/auth/email

Needs: user. Body: `newEmail`, `password` (or a fresh confirmation in its place), `redirectTo` (optional). Answer: `202 { "sent": true }`.

Errors: `400 bad_request`, `403 confirm_first`, `409 site_url_required`, `409 email_in_use`, `503 email_not_ready`.

### POST /v1/auth/reauthenticate

Needs: user. No body. Emails a six-digit code. Answer: `202 { "sent": true }`.

### POST /v1/auth/reauthenticate/verify

Needs: user. Body: `code`. Answer: `{ "ok": true, "validForMinutes": 10 }`.

Errors: `401 bad_code`.

## Data

Guide: [The data API](https://rezincore.com/docs/database/data-api.md).

| Route | Needs | Body | Answer |
|---|---|---|---|
| `GET /v1/data/:table` | caller | | `{ "rows": [ … ] }` |
| `POST /v1/data/:table` | caller | One row, or a list of up to 500 | `201 { "rows": [ … ] }` |
| `PATCH /v1/data/:table?<filter>` | caller | The values to change | `{ "rows": [ … ] }` |
| `DELETE /v1/data/:table?<filter>` | caller | | `{ "deleted": 3 }` |

Query for `GET`: filters as `column=operator.value` (`eq`, `neq`, `gt`, `gte`, `lt`, `lte`, `like`, `ilike`, `in.(a,b)`, `is.null`, `is.notnull`), `select=a,b`, `order=a.asc,b.desc`, `limit` (1 to 1000, default 100), `offset`.

`PATCH` and `DELETE` take the same filters and require at least one.

Values come back as JSON. A `bigint` comes back as a string, and bytes as `\x` followed by hex.

Errors: `400 bad_request`, `400 filter_required`, `400 organization_required`, `401 sign_in_required`, `401 signed_out`, `403 not_allowed`, `403 password_change_required`, `404 not_found`, `408 timeout`, `409 conflict`.

## Storage

Guide: [Storage](https://rezincore.com/docs/storage.md).

| Route | Needs | Body | Answer |
|---|---|---|---|
| `PUT /v1/storage/:bucket/<path>` | caller | The file | `201 { "bucket", "path", "bytes", "contentType" }` |
| `GET /v1/storage/:bucket?prefix=` | caller | | `{ "files": [ { "path", "bytes", "contentType", "createdAt" } ] }` |
| `GET /v1/storage/:bucket/<path>` | caller | | The file, as a download |
| `DELETE /v1/storage/:bucket/<path>` | caller | | `{ "ok": true }` |
| `POST /v1/storage/:bucket/sign` | caller | `path`, `expiresIn` (10 to 604800 seconds; default 3600) | `{ "url", "expiresAt" }` |
| `GET /v1/storage/signed/:project/:bucket/:organization/<path>?expires=&sig=` | nothing | | The file |
| `GET /v1/storage/public/:project/:bucket/:organization/<path>` | nothing | | The file, if the bucket is public |

`:project` is the project ID (twenty lowercase letters).

Errors: `400 bad_request`, `400 not_accepted`, `403 server_only`, `403 bad_link`, `404 not_found`.

## Functions

Guide: [Functions](https://rezincore.com/docs/functions.md).

| Route | Needs | Answer |
|---|---|---|
| `GET`, `POST`, `PUT`, `PATCH`, `DELETE` `/functions/v1/<name>` and any path below it | key; a user's token too if the function takes signed-in users only | The function's own status, headers and body |

`Set-Cookie` and cross-origin headers are dropped from the answer, a page type is sent as `text/plain`, and `x-rezin-function-run` names the run.

Errors: `401 bad_key`, `401 sign_in_required`, `404 not_found`, `429 too_many_requests`, `429 busy`, `500 function_error`, `502 function_memory`, `502 function_output`, `502 function_crashed`, `504 function_timeout`.

## Realtime

Guide: [Realtime](https://rezincore.com/docs/realtime.md).

```
wss://api.rezincore.com/v1/realtime
```

| You send | You receive |
|---|---|
| `{"type":"auth","apikey":"…","token":"…"}` or `{"type":"auth","apikey":"<secret key>","organization":"<id>"}` | `{"type":"ready"}` |
| `{"type":"subscribe","table":"notes"}` | `{"type":"subscribed","table":"notes"}` |
| `{"type":"ping"}` | `{"type":"pong"}` |
| | `{"type":"change","table":"notes","op":"INSERT" or "UPDATE","row":{…}}` |
| | `{"type":"change","table":"notes","op":"DELETE","key":{…}}` |
| | `{"type":"signed_out"}`, then the socket closes |

## Browsers

The API answers cross-origin requests only for origins on the project's list (Site URL and Redirect URLs). Allowed request headers: `apikey`, `authorization`, `content-type`, `x-organization-id`. Allowed methods: `GET`, `POST`, `PUT`, `PATCH`, `DELETE`. Credentials (cookies) are never allowed across origins; keys and tokens identify the caller.

## Not part of this API

- Tools and scripts that act as *you*: the [management API](https://rezincore.com/docs/reference/management-api.md) and the [connector](https://rezincore.com/docs/tools/connector.md), with a personal access token.
- Changing or resetting a user's password. Not built.
- Sending your own emails, and reading a secret's value. Not built. (A function is given the secrets assigned to it.)
