Browse and search developers

OAuth apps

Authorization code flow with PKCE

Send users to the FormitAI consent screen, exchange the code for tokens, call the REST API and refresh tokens safely.

On this page
  1. 1. Send the user to FormitAI
  2. 2. Handle the callback
  3. 3. Exchange the code for tokens
  4. 4. Refresh before the access token expires
  5. Errors

1. Send the user to FormitAI

Redirect the user’s browser to the authorization URL with your client ID, an exact registered redirect URI, the scopes separated by spaces, a random state you check later, and a PKCE challenge. The user signs in if needed, chooses a workspace and approves.

Node.js
import { createHash, randomBytes } from 'node:crypto';

const verifier = randomBytes(32).toString('base64url');
const challenge = createHash('sha256').update(verifier).digest('base64url');
const state = randomBytes(16).toString('hex');
// Store verifier and state in the user's session.

const url = new URL('https://api.formitai.com/v1/oauth/authorize');
url.search = new URLSearchParams({
  client_id: process.env.FORMIT_CLIENT_ID,
  redirect_uri: 'https://example.com/oauth/formit/callback',
  response_type: 'code',
  scope: 'forms:read responses:read',
  state,
  code_challenge: challenge,
  code_challenge_method: 'S256',
}).toString();
res.redirect(url.toString());

2. Handle the callback

FormitAI redirects back with code and your state, or with error=access_denied if the user cancelled. Reject the request if state does not match. Codes work once and expire after 10 minutes.

3. Exchange the code for tokens

curl https://api.formitai.com/v1/oauth/token \
  -u "$FORMIT_CLIENT_ID:$FORMIT_CLIENT_SECRET" \
  -d grant_type=authorization_code \
  -d code=CODE_FROM_CALLBACK \
  -d redirect_uri=https://example.com/oauth/formit/callback \
  -d code_verifier=VERIFIER_FROM_SESSION

Store both tokens encrypted, together with workspace_id. Call the REST API with Authorization: Bearer fmt_oat_…; GET /v1/me shows which workspace and app a token belongs to.

4. Refresh before the access token expires

cURL
curl https://api.formitai.com/v1/oauth/token \
  -u "$FORMIT_CLIENT_ID:$FORMIT_CLIENT_SECRET" \
  -d grant_type=refresh_token \
  -d refresh_token=fmt_ort_…

Errors

The token and revoke endpoints return standard OAuth errors as {"error": "...", "error_description": "..."}: invalid_client (401), and invalid_request, invalid_grant, invalid_scope or unsupported_grant_type (400). An unregistered redirect_uri is shown as an error on the authorize page instead of redirecting, so a bad URI can never receive a code.

Ready?

Build your first AI form in minutes.

No credit card. Generate from a prompt, enable follow-ups, and publish a link or embed the same day.