Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ The CORS policy is built in `src/config/cors.ts` when the app starts and depends

- **`FRONTEND_URL` is required in production.** Set it to the frontend origin, e.g. `FRONTEND_URL=https://app.flowfi.xyz`. Comma-separate several origins (e.g. preview domains). Each entry is normalised to its origin, so `https://app.flowfi.xyz/` works too. If no valid origin is configured, or any entry is not an absolute `http(s)` URL (including `*`), the server throws at startup instead of running with an open policy.
- **Set `NODE_ENV=production` in every deployed environment.** Otherwise the development policy applies (headers and methods unrestricted). The server logs a warning at startup when `NODE_ENV` is unset.
- Browser requests from any other origin get `403 {"error": "CORS origin not allowed"}` and no `Access-Control-Allow-Origin` header. The rejected origin is logged at `warn` level.
- Browser requests from any other origin get `403 {"error": {"code": "FORBIDDEN", "message": "CORS origin not allowed"}}` and no `Access-Control-Allow-Origin` header. The rejected origin is logged at `warn` level.
- Requests without an `Origin` header (health checks, webhooks, curl, server-to-server calls) are not affected.

## Prisma Database
Expand Down Expand Up @@ -99,6 +99,24 @@ If a `prisma generate` / `prisma migrate` command misbehaves, check `prisma.conf

All REST API endpoints are prefixed with `/v1`. Refer to the API Documentation in the root `README.md` and the `docs/` folder for versioning and authentication details.

### Error envelope

Every non-2xx JSON response has the same body, produced only by `sendApiError` (`src/lib/api-error.ts`) and by `ApiError` → `errorHandler` for thrown errors:

```json
{
"error": {
"code": "VALIDATION_ERROR",
"message": "Request validation failed",
"details": [{ "path": ["sender"], "message": "Sender address is required", "code": "too_small" }]
}
}
```

- `code` is stable and machine-readable — branch on it. `message` is display text and may change.
- `details` is present only when there is something to report and its shape depends on `code`: the failing zod issues for `VALIDATION_ERROR`, an object of remediation hints elsewhere.
- This covers 4xx and 5xx from every layer, including the rate limiter's `429 RATE_LIMITED`, the body-size `413 PAYLOAD_TOO_LARGE` and the CORS `403 FORBIDDEN`. The spec's `components.schemas.Error` in `swagger/flowfi.openapi.json` is the generated contract.

## Server-Sent Events (SSE)

The backend exposes an SSE endpoint (`/v1/streams/events`) to stream real-time updates to the frontend whenever on-chain stream events are indexed.
Expand Down
33 changes: 22 additions & 11 deletions backend/docs/API_VERSIONING.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,23 @@ X-API-Migration-Path: /v1/streams

### Example Deprecated Response

Sunset responses use the same envelope as every other error; see
[DEPRECATION_POLICY.md](./DEPRECATION_POLICY.md) for the contract and for the fact
that no endpoint emits it yet.

```json
{
"error": "Deprecated endpoint",
"message": "This endpoint has been deprecated. Please use /v1/streams instead.",
"deprecated": true,
"migration": {
"old": "/streams",
"new": "/v1/streams"
},
"sunsetDate": "2024-12-31"
"error": {
"code": "ENDPOINT_DEPRECATED",
"message": "This endpoint has been deprecated. Please use /v1/streams instead.",
"details": {
"migration": {
"old": "/streams",
"new": "/v1/streams"
},
"sunsetDate": "2024-12-31"
}
}
}
```

Expand Down Expand Up @@ -221,9 +228,13 @@ GET /v2/streams
Response:
```json
{
"error": "Unsupported API version",
"message": "API version 'v2' is not supported. Supported versions: v1",
"supportedVersions": ["v1"]
"error": {
"code": "UNSUPPORTED_API_VERSION",
"message": "API version 'v2' is not supported. Supported versions: v1",
"details": {
"supportedVersions": ["v1"]
}
}
}
```

Expand Down
24 changes: 20 additions & 4 deletions backend/docs/AUTHENTICATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,37 @@ const response = await fetch('http://localhost:3001/v1/users/me', {

## Error Responses

Every non-2xx response, including the two 401s below, uses the shared error envelope:

```json
{
"error": {
"code": "STRING_CODE",
"message": "human readable text",
"details": "optional; omitted when there is nothing to report"
}
}
```

### 401 Unauthorized - Missing Token

```json
{
"error": "Unauthorized",
"message": "Missing or invalid Authorization header. Expected format: Bearer <signed_transaction>"
"error": {
"code": "UNAUTHORIZED",
"message": "Missing Bearer token"
}
}
```

### 401 Unauthorized - Invalid Signature

```json
{
"error": "Unauthorized",
"message": "Invalid or expired signature"
"error": {
"code": "UNAUTHORIZED",
"message": "Invalid or expired token"
}
}
```

Expand Down
24 changes: 16 additions & 8 deletions backend/docs/DEPRECATION_POLICY.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,24 @@ X-API-Migration-Path: /v1/streams
```

**Body:**

> Not yet implemented — no endpoint in `src/` returns `410` or sets the
> `X-API-*` headers. This is the shape such a response must use once it exists,
> written against the envelope every other error already returns.

```json
{
"error": "Deprecated endpoint",
"message": "This endpoint has been deprecated. Please use /v1/streams instead.",
"deprecated": true,
"migration": {
"old": "/streams",
"new": "/v1/streams"
},
"sunsetDate": "2024-12-31"
"error": {
"code": "ENDPOINT_DEPRECATED",
"message": "This endpoint has been deprecated. Please use /v1/streams instead.",
"details": {
"migration": {
"old": "/streams",
"new": "/v1/streams"
},
"sunsetDate": "2024-12-31"
}
}
}
```

Expand Down
14 changes: 9 additions & 5 deletions backend/docs/SANDBOX_MODE.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,15 @@ If an endpoint requires sandbox mode and the request is not sandboxed:

```json
{
"error": "Sandbox mode required",
"message": "This endpoint requires sandbox mode.",
"hint": {
"header": "X-Sandbox-Mode: true",
"queryParam": "?sandbox=true"
"error": {
"code": "SANDBOX_MODE_REQUIRED",
"message": "This endpoint requires sandbox mode. Add X-Sandbox-Mode: true header or ?sandbox=true query parameter.",
"details": {
"hint": {
"header": "X-Sandbox-Mode: true",
"queryParam": "?sandbox=true"
}
}
}
}
```
Expand Down
Loading
Loading