> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nodeloc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Accounts and Sessions

> Sign in, complete two-factor authentication, edit profiles, and manage sessions with the NodeLoc v1 API

Use the `/api/v1` prefix on your NodeLoc site, for example `https://www.nodeloc.com/api/v1/me`. For requests with a body, send `Content-Type: application/json` and a single JSON object. Use the API path without a language prefix.

## Choose a session credential

| Client | Authentication |
| - | - |
| Same-origin web page | After sign-in, the browser stores the `nl_session` cookie and sends it with subsequent requests. The cookie uses `HttpOnly` and `SameSite=Lax`, with `Secure` when the site's public URL uses HTTPS. |
| Native client | Store the `token` from the login response in the operating system's secure storage. Send it as `Authorization: Bearer <token>` on subsequent requests. |

Both credentials represent the same type of NodeLoc session. Send one credential per request. If you send both, a nonempty session cookie takes precedence, even if it has expired. A session expires 60 days after creation. Activity leaves that expiry unchanged. Send browser writes from the same origin. The server checks browser origin information, and native HTTPS requests need no separate CSRF token.

The general `/api/v1` API uses account sessions. The [app development CLI](https://docs.nodeloc.com/en/miniprogram/quickstart.md) uses its `Api-Key` with app platform endpoints. To offer “Sign in with NodeLoc” on another website, use the [OAuth 2.0 / OpenID Connect flow](https://docs.nodeloc.com/en/api-reference/introduction.md) and use its access token with the OAuth endpoints.

## Sign in with a password

Send the username or email address and password to `POST /api/v1/auth/login`. This example shows an initial request with placeholders.

```json theme={null}
{
  "login": "alex@example.test",
  "password": "<password>"
}
```

| Field | Type and purpose |
| - | - |
| `login` | Required string. The username or email address. |
| `password` | Required string. The account password. |
| `code` | The six-digit authenticator code or a one-time backup code for two-factor authentication. Send it as a string to preserve leading zeroes. |
| `trust_device` | Boolean. Lets the user trust the device for 30 days when submitting a two-factor code. |
| `captcha` | String. The verification token returned by the configured CAPTCHA provider when required. |
| `device` | Optional string identifying the client device. |
| `remember` | Optional boolean, defaulting to `true`. Set it to `false` to use a cookie that expires when the browser session ends. The server-side session lifetime stays the same. |

A successful login returns HTTP `200` with a `user` object and sets the session cookie. A native request without `Sec-Fetch-Mode` also receives a session `token` in JSON. Browser clients can rely on the cookie. Here is a native success response.

```json theme={null}
{
  "user": {
    "id": 42,
    "username": "alex",
    "name": "Alex",
    "role": "user",
    "trust_level": 0,
    "tier_name": "青铜",
    "title": null,
    "created_at": "2026-10-12T08:00:00Z",
    "locale": "en"
  },
  "token": "<session-token>"
}
```

### Handle two-factor authentication and CAPTCHA

1. If the login returns HTTP `401` with `error.code` set to `second_factor_required`, prompt for an authenticator or backup code. Repeat the login request with the original `login` and `password` plus the new `code`. An incorrect or previously used code returns `second_factor_invalid`.
2. To remember the device at the user's request, send `"trust_device": true` with the code. The browser receives a trusted-device cookie. A native client also receives `trust_token` in JSON. For later logins to the same account, a native client can send this token in `X-Nodeloc-Trust` and omit the code while the trust remains valid. The token lasts 30 days, and the password is still required.
3. Read `GET /api/v1/auth/captcha` for site settings. When enabled, use `provider`, `site_key`, `actions.login`, `login_mode`, and `login_after_failures`. If the server returns `captcha_required` or `captcha_invalid`, complete the configured challenge and send the new token as `captcha`.

Native apps using the site's sign-in options can read `GET /api/v1/auth/providers`. In NodeLoc's system-browser sign-in flow, send the returned one-time `code` to `POST /api/v1/auth/app/exchange`. Include the matching `code_verifier` if the flow started with a PKCE challenge. A successful exchange returns `200` with `user` and `token`. The code expires after two minutes. An expired code or failed verification returns `400 invalid_code`. Start sign-in again to get a new code.

## Read or update a profile

Call `GET /api/v1/me` with a session credential. This native-client example uses the site URL in `BASE_URL` and the current session token in `NODELOC_SESSION_TOKEN`.

```bash theme={null}
curl --fail-with-body --silent --show-error \
  --header "Authorization: Bearer $NODELOC_SESSION_TOKEN" \
  "$BASE_URL/api/v1/me"
```

This partial response shows the commonly used fields.

```json theme={null}
{
  "user": {
    "id": 42,
    "username": "alex",
    "name": "Alex",
    "role": "user",
    "trust_level": 0,
    "tier_name": "青铜",
    "title": null,
    "created_at": "2026-10-12T08:00:00Z",
    "locale": "en",
    "email": "alex@example.test",
    "email_verified": true,
    "bio_raw": "Writing Go.",
    "website": "https://alex.example.test",
    "location": "",
    "avatar_url": null
  }
}
```

`GET /api/v1/me/account` returns an `account` object with `phone`, `has_password`, `password_changed_at`, `created_at`, `pending_email`, and `cover_url`. For public profiles, `GET /api/v1/users/{username}` returns `user` and `stats`. If the user hides their level and reputation, responses to other viewers omit `trust_level`, `tier_name`, and `stats.votes_received`. An unknown user returns `404 not_found`.

Use `PUT /api/v1/me` to edit a profile. Read the current profile first and send each value you want to keep. An omitted field or empty string clears the corresponding value. The request field is `bio`, and the raw text in the read response is `bio_raw`.

```json theme={null}
{
  "name": "Alex",
  "bio": "Writing **Go**.",
  "website": "https://alex.example.test",
  "location": "Singapore"
}
```

The limits are 50 characters for the display name, 3,000 for the bio, and 100 for the location. A website URL can contain up to 300 bytes and must start with `http://` or `https://`. A successful update returns `200` with a basic `user` object. Call `GET /api/v1/me` to retrieve the full updated profile. A field validation error returns `422 invalid` with the reason keyed by request field in `error.fields`.

## Verify identity and manage sessions

When an operation returns `403 reauth_required`, send `{"password":"<password>"}` or `{"code":"<one-time-code>"}` to `POST /api/v1/me/reauth` with the current session. A successful request returns `200` and `verified_until`. Retry the original operation in the same session within 10 minutes. To manage two-factor authentication, `GET /api/v1/me/two-factor` returns `enabled` and `backup_codes_left`. Send a `POST` to the same path to get the authenticator `secret` and `uri`, then submit `{"code":"<authenticator-code>"}` in a `PUT` to enable it. The response contains 10 `backup_codes`. Show the secret and backup codes only to the current user and keep them out of logs.

| Task | Request | Success |
| - | - | - |
| List active sessions for this account | `GET /api/v1/me/sessions` | `200` with a `sessions` array. |
| Revoke a specific session | `DELETE /api/v1/me/sessions/{id}` | `204` with an empty body. An unknown session or one belonging to another account returns `404 not_found`. |
| Revoke other sessions | `DELETE /api/v1/me/sessions` | `200`, for example `{"revoked":2}`. The current session stays active. The account's trusted devices are cleared. |
| Sign out of the current session | `POST /api/v1/auth/logout` | `204` with an empty body. The server invalidates the supplied session and clears the session cookie. A request without a session also returns `204`. |

Each session entry has an `id`, `user_agent`, `ip`, `last_seen_at`, `created_at`, and `current`. Use the numeric `id` to revoke a specific session. A native client should remove its stored session token after signing out.

## Handle errors

Use the HTTP status and `error.code` to choose the next action. Display `message` to the user because its wording can vary with locale and account state.

```json theme={null}
{
  "error": {
    "code": "second_factor_required",
    "message": "Enter your two-factor authentication code"
  }
}
```

| HTTP | `error.code` | Client action |
| - | - | - |
| `400` | `invalid_json` | Check JSON syntax and field names. |
| `401` | `unauthorized` | Sign in to obtain a valid session for the protected endpoint. |
| `401` | `invalid_credentials` | Ask the user to check their username or email address and password. |
| `401` | `second_factor_required`, `second_factor_invalid` | The login still needs verification. Prompt for a valid authenticator code or an unused backup code. |
| `403` | `email_not_verified`, `pending_approval`, `registration_rejected` | Prompt for email verification or explain the registration review status. |
| `403` | `suspended` | The account is suspended. Read `until` and the `permanent` flag. |
| `403` | `cross_origin` | Check the origin of the browser write. |
| `422` | `captcha_required`, `captcha_invalid` | Complete the configured CAPTCHA and send a fresh token. |
| `429` | `rate_limited` | Wait for the number of seconds in `Retry-After` before retrying. |
| `503` | `captcha_unavailable` | CAPTCHA is temporarily unavailable. Offer to try again later. |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.