Skip to content
Documentation
Hevia for developers

Build an app with OAuth

Let any Hevia business connect your product: authorize, exchange, call the API, refresh.

You need: a registered app manifest with your redirect URI, and an OAuth client (see OAuth 2.0 and PKCE).

1. Start the connection

TypeScript
import { buildAuthorizeUrl, generatePkce, generateState } from "@hevia/sdk";

const { verifier, challenge } = generatePkce();
const state = generateState();
session.set({ verifier, state }); // server-side session
const url = buildAuthorizeUrl({ clientId: "hc_...", redirectUri: "https://app.example.com/callback", scopes: ["contacts:read", "deals:read"], state, codeChallenge: challenge });
redirect(url);

2. Handle the callback

TypeScript
import { exchangeCode } from "@hevia/sdk";

if (query.state !== session.state) throw new Error("state mismatch");
const tokens = await exchangeCode({ clientId: "hc_...", redirectUri: "https://app.example.com/callback", code: query.code, codeVerifier: session.verifier });
await db.saveTokens(tokens); // access_token, refresh_token, hevia_business

3. Call the API for that business

TypeScript
import { HeviaClient, OAuthTokenManager } from "@hevia/sdk";

const manager = new OAuthTokenManager({ clientId: "hc_...", tokens, onTokens: (t) => db.saveTokens(t) });
const hevia = new HeviaClient({ tokenProvider: manager.tokenProvider });
const { data } = await hevia.deals.list({ limit: 10 });

4. When the business disconnects

Calls answer 401 unauthorized and a refresh answers invalid_grant. Mark the connection as ended and offer to connect again. Never retry in a loop.

Warning

Store the rotated refresh token from onTokens every time. Reusing an old one ends the whole grant.

Generated from the product's own registries, so the reference matches the API.