Guide

User accounts

Every project comes with sign-up, sign-in and profiles for the people who use your app. Here is every endpoint, and how to keep users signed in safely.

How it works

Each project has a users entity, created with the project. Your app's users sign up and sign in through the endpoints below, on your project's URL. Like every API request, they need your API key in the X-API-Key header.

Signing in returns an access token. Send it with every request a signed-in user makes, as Authorization: Bearer ACCESS_TOKEN. Your access rules then decide what that user may read and change.

EndpointWhat it doesSigned in?
POST /auth/registerCreate an account and sign inNo
POST /auth/loginSign in with email and passwordNo
POST /auth/refreshSwap a refresh token for new tokensNo
POST /auth/logoutEnd this sign-in (revokes the refresh token)No
GET /auth/meThe signed-in userYes
PATCH /auth/meUpdate your own profileYes
POST /auth/change-passwordChange password, sign out other devicesYes
POST /auth/forgot-passwordEmail a password reset linkNo
POST /auth/reset-passwordSet a new password with the token from the emailNo
POST /auth/verify-emailConfirm an email address with the token from the emailNo
POST /auth/resend-verificationEmail a new confirmation linkNo

All paths start with /api/v1, for example https://YOUR_PROJECT.codefreeapi.com/api/v1/auth/login.

Sign up

Send an email address and a password of at least 6 characters. full_name is optional. New accounts always get the role user; you make someone an admin from the dashboard.

bash
curl -X POST https://YOUR_PROJECT.codefreeapi.com/api/v1/auth/register \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"email": "[email protected]", "password": "secret123", "full_name": "Ana"}'

The answer is 201 Created with the user and their tokens, the same as for sign in. The user is signed in straight away.

Sign in

bash
curl -X POST https://YOUR_PROJECT.codefreeapi.com/api/v1/auth/login \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"email": "[email protected]", "password": "secret123"}'
200 OK
{
  "message": "Login successful",
  "accessToken": "eyJhbGciOiJIUzI1NiIs…",
  "refreshToken": "9f3c2a…",
  "expiresIn": 900,
  "token": "eyJhbGciOiJIUzI1NiIs…",
  "user": {
    "id": "5b0e…",
    "email": "[email protected]",
    "role": "user",
    "full_name": "Ana",
    "createdAt": "2026-10-23T09:12:00.000Z",
    "updatedAt": "2026-10-23T09:12:00.000Z"
  }
}

The user object contains every field of your users entity except the password. Wrong email or wrong password both answer 401 with "Invalid credentials", so nobody can find out which emails have accounts.

Tokens and refreshing

TokenValid forUse it for
accessToken15 minutesThe Authorization: Bearer … header of every request
refreshToken30 days, onceGetting a new pair from POST /auth/refresh
token7 daysOlder apps only. Prefer the access and refresh tokens

When a request answers 401 because the access token expired, send the refresh token to /auth/refresh. You get a new access token and a new refresh token. Each refresh token works once, so always store the new one.

bash
curl -X POST https://YOUR_PROJECT.codefreeapi.com/api/v1/auth/refresh \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"refreshToken": "9f3c2a…"}'

A refresh token that is used twice ends every session

If an old refresh token comes back, it was probably copied by someone else. CodeFreeAPI then signs the user out everywhere, and they need to sign in again. Make sure two requests in your app never refresh at the same time with the same token.

A small helper for your frontend

This plain JavaScript works in React, Vue or any other framework. It keeps the tokens, adds the headers and refreshes once when a request answers 401. AI app builders can adapt it for you.

api.js
const API = "https://YOUR_PROJECT.codefreeapi.com/api/v1";
const API_KEY = "cfa_your_key";

let session = JSON.parse(localStorage.getItem("session") || "null");

function saveSession(data) {
  session = { accessToken: data.accessToken, refreshToken: data.refreshToken };
  localStorage.setItem("session", JSON.stringify(session));
}

export async function login(email, password) {
  const res = await fetch(`${API}/auth/login`, {
    method: "POST",
    headers: { "Content-Type": "application/json", "X-API-Key": API_KEY },
    body: JSON.stringify({ email, password }),
  });
  if (!res.ok) throw new Error((await res.json()).error);
  const data = await res.json();
  saveSession(data);
  return data.user;
}

async function refresh() {
  const res = await fetch(`${API}/auth/refresh`, {
    method: "POST",
    headers: { "Content-Type": "application/json", "X-API-Key": API_KEY },
    body: JSON.stringify({ refreshToken: session?.refreshToken }),
  });
  if (!res.ok) {
    session = null;
    localStorage.removeItem("session");
    throw new Error("Signed out, please sign in again");
  }
  saveSession(await res.json());
}

// Every API call goes through this: it adds the keys and refreshes once on 401
export async function api(path, options = {}) {
  const send = () =>
    fetch(`${API}${path}`, {
      ...options,
      headers: {
        "Content-Type": "application/json",
        "X-API-Key": API_KEY,
        ...(session ? { Authorization: `Bearer ${session.accessToken}` } : {}),
        ...options.headers,
      },
    });

  let res = await send();
  if (res.status === 401 && session?.refreshToken) {
    await refresh();
    res = await send();
  }
  return res;
}

// Usage
// await login("[email protected]", "secret123");
// const tasks = await (await api("/tasks")).json();
localStorage keeps the example short. Anything on your page can read it, so keep your frontend free of third-party scripts you don't trust.

Sign out

Send the refresh token to POST /auth/logout and delete both tokens in your app. The refresh token stops working at once; the access token still works until it expires, at most 15 minutes later.

bash
curl -X POST https://YOUR_PROJECT.codefreeapi.com/api/v1/auth/logout \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"refreshToken": "9f3c2a…"}'

Profile

GET /auth/me returns { "user": { … } } for the signed-in user. PATCH /auth/me updates the fields you send. Add your own fields, such as avatar or bio, to the users entity in the dashboard and users can edit them here. Sending null clears a field.

bash
curl -X PATCH https://YOUR_PROJECT.codefreeapi.com/api/v1/auth/me \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"full_name": "Ana Petrović", "bio": "Runner"}'

email, role and the password can't be changed this way; trying answers 403.

Passwords

Change password

POST /auth/change-password with currentPassword and newPassword. Every other device is signed out, and the answer contains fresh tokens for the current one.

Forgotten password

POST /auth/forgot-password with { "email": "…" }. CodeFreeAPI emails a link to a reset page it hosts for you, at codefreeapi.com/app/YOUR_PROJECT/reset-password. The link works for one hour and only once. After a reset, the user is signed out everywhere.

The answer is always the same, whether or not the account exists, so the form can't be used to discover who has an account.

Email limits during the beta

Each project can send up to 20 password reset and confirmation emails per day. The emails come from CodeFreeAPI with your project's name.

Email confirmation

Off by default. Turn on Require email confirmation in Project Settings → User accounts, and new users must open a link from their inbox before they can sign in:

  • POST /auth/register answers 201 with emailVerificationRequired: true and the user, but no tokens. Show a "check your inbox" message.
  • Signing in before confirming answers 403 with code: "EMAIL_NOT_VERIFIED". Offer a button that calls POST /auth/resend-verification with { "email": "…" }.
  • The link works for 3 days, once. It opens a page CodeFreeAPI hosts, or your own page (below).
  • GET /auth/me includes emailVerified.

Users who signed up before you turned it on keep working; they are not asked to confirm.

Sign in with Google

Users can sign in with their Google account instead of a password. It uses your own Google client, so the consent screen shows your app's name.

  1. In Google Cloud, create an OAuth client ID of type Web application. Add the Authorized redirect URI shown in Project Settings → Sign in with Google.
  2. Paste its client ID and secret in Project Settings, and list the pages of your app that users come back to, for example https://myapp.com/auth/google.
  3. Send the browser to the start URL. After signing in, Google sends it back to your page with a one-time code.
  4. Trade the code for the same tokens as a password sign-in (within 2 minutes, once):
js
const API = "https://codefreeapi.com/api/v1";

// 1. Your "Sign in with Google" button
const back = encodeURIComponent("https://myapp.com/auth/google");
location.href = `${API}/auth/google/start?project=YOUR_PROJECT_SLUG&redirect_uri=${back}`;

// 2. On https://myapp.com/auth/google
const code = new URLSearchParams(location.search).get("code");
const res = await fetch(`${API}/auth/google/exchange`, {
  method: "POST",
  headers: { "X-API-Key": API_KEY, "Content-Type": "application/json" },
  body: JSON.stringify({ code }),
});
const { accessToken, refreshToken, user } = await res.json();

The first Google sign-in creates the user (with the name from Google). If a user with that email already exists, they are signed in to that account, and their email counts as confirmed. If something went wrong, your page gets ?error= instead of a code: access_denied (the user cancelled), email_not_verified or google_error.

Your own pages

By default, the reset and confirmation links open pages on codefreeapi.com. To keep users in your app, set your own addresses under Project Settings → User accounts (https, or http on localhost while developing). The email link becomes your address with ?token=… added. Your page then calls:

js
const token = new URLSearchParams(location.search).get('token');

// Reset page: after the user typed a new password
await fetch('https://YOUR_PROJECT.codefreeapi.com/api/v1/auth/reset-password', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ token, password: newPassword }),
});

// Confirmation page: right away
await fetch('https://YOUR_PROJECT.codefreeapi.com/api/v1/auth/verify-email', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ token }),
});

Both work without the API key: the token identifies the project. They answer 400 for a used or expired token.

Errors

StatusWhen
400Invalid input (the details array says which field), or the email is already registered
401Wrong email or password, missing or expired access token, or an invalid refresh token
403A field that can't be changed through PATCH /auth/me, or EMAIL_NOT_VERIFIED when signing in before confirming
429Too many attempts. Sign-in, sign-up and password endpoints are rate limited

Every error has the same shape: { "error": "message" }, sometimes with details.