Skip to main content
These functions take the client ID of an app registered at devs.aveid.net. New to Ave? Start with the quickstart.

Import paths

See @ave-id/sdk for every import path.
Never use exchangeCodeServer or refreshTokenServer in browser code. They require your clientSecret, which must never be exposed to the client.

Config object

Most functions take a config object as the first argument:

PKCE helpers

generateCodeVerifier()

Generates a cryptographically random PKCE code verifier.
Returns a 43-character base64url-encoded random string. Store this in sessionStorage before redirecting and send it as codeVerifier when exchanging the code.

generateCodeChallenge(verifier)

Generates the PKCE code challenge from a verifier using SHA-256.
string
required
The code verifier from generateCodeVerifier().
Returns Promise<string> — the base64url-encoded SHA-256 hash. Pass this as codeChallenge to buildAuthorizeUrl.

generateNonce()

Generates a random nonce for ID token replay protection.
Returns a 32-character base64url-encoded random string. Pass it as nonce to buildAuthorizeUrl and verify it matches the nonce claim in the id_token after exchange.

Authorization functions

buildAuthorizeUrl(config, params)

Builds the URL to redirect users to Ave for sign-in.
string
required
Your app’s client ID.
string
required
The registered redirect URI the user will be sent to after authorization.
string[]
default:"[\"openid\", \"profile\", \"email\"]"
Scopes to request: "openid", "profile", "email", "offline_access", "user_id", and one of "e2ee:symmetric" or "e2ee:asymmetric", optionally with "e2ee:reset". See Scopes and claims.
string
CSRF protection token. Generate with crypto.randomUUID(). Store and verify on the callback.
string
ID token replay protection. Generate with generateNonce(). Verify against the nonce claim in id_token.
string
PKCE challenge from generateCodeChallenge(). Required for public clients.
"none" | "login" | "consent" | "select_account" | string
OAuth-style interaction hint. Use select_account when your app offers Switch account or Sign out so Ave always shows the swipe-to-authorize screen and identity picker instead of silently reusing the last authorization. Combine values with a space (for example consent select_account).
object
Extra query parameters on the Ave sign-in URL. Encryption is requested with scopes, not extra parameters; see App encryption.

buildSwitchAccountUrl(config, params?)

Convenience wrapper around buildAuthorizeUrl that sets prompt=select_account. Use from your app’s Switch account or Use a different profile action so Ave skips instant re-authorization and always shows the identity picker plus swipe-to-authorize UI.
For embed flows, pass extraParams: { prompt: "select_account" } to openAveSheet / openAvePopup instead.

exchangeCode(config, payload)

Exchanges an authorization code for tokens. Use for PKCE (public client) flows.
string
required
The authorization code from the callback URL.
string
The PKCE code verifier from sessionStorage. Required if you used PKCE.

refreshToken(config, payload)

Exchanges a refresh token for a new set of tokens.
string
required
The refresh token from a previous token response.
Refresh tokens rotate on every use — each successful refresh returns a new refresh token and invalidates the old one. Store the new token immediately after every refresh. Sending an old token returns invalid_grant and may revoke the entire token family.

fetchUserInfo(config, accessToken)

Fetches live identity claims from the userinfo endpoint.
string
required
The opaque access_token (not the JWT) from the token response.
Returns UserInfo with sub, name?, preferred_username?, email?, picture?.

Client helpers

Import from @ave-id/sdk/client. Browser-only.

startPkceLogin(params)

Generates PKCE params, saves them to sessionStorage, and redirects the user to Ave. The browser-friendly alternative to calling generateCodeVerifier + buildAuthorizeUrl + window.location.href manually.
Automatically generates a PKCE verifier/challenge, a nonce for ID token replay protection, and a state value for CSRF protection — unless you supply your own. All three are stored in sessionStorage and retrieved automatically by finishPkceLogin.

finishPkceLogin(options)

Handles the OAuth callback: reads stored PKCE state, verifies the returned state against the stored value, exchanges the authorization code, merges encryption keys from the URL fragment, verifies the returned tokens, removes keys from the address bar, and cleans up sessionStorage. Returns null when no code parameter is present — safe to call on every page load.

completeOAuthCallback(session, options)

Runs finishPkceLogin with the same options, then saves the tokens and any encryption keys with session.setTokensFromResponse. Returns the token response, or null when the URL has no code.

JWT verification

These functions are available in @ave-id/sdk, @ave-id/sdk/client, and @ave-id/sdk/server.

verifyJwt(token, options?)

Verifies an Ave-issued JWT: parses the token, fetches the OIDC discovery document, validates the RS256 signature against the JWKS, and checks issuer, expiry, audience, and nonce claims. Returns the decoded payload on success or null on any validation failure.
string
required
The raw JWT string to verify.
string | string[]
Expected aud claim. Pass your clientId when verifying id_token. Pass "https://aveid.net" when verifying access_token_jwt.
string
Expected nonce claim. Pass the nonce you sent in the authorization request to prevent replay attacks.
number
default:"30"
Tolerance in seconds applied to exp and nbf checks to handle clock drift between servers.
Returns Promise<T | null> — the decoded payload typed as T, or null if any check fails (invalid signature, expired, issuer mismatch, audience mismatch, nonce mismatch, or malformed token).
verifyJwt fetches the OIDC discovery document and JWKS automatically and caches both for 5 minutes. For server environments with no global fetch, pass a fetcher option.

fetchJwks(options?)

Fetches and caches the Ave JWKS (JSON Web Key Set). Used internally by verifyJwt. Useful if you need direct access to the signing keys.

Server helpers

Import from @ave-id/sdk/server.

verifyAveIdToken(token, options)

Verifies an Ave id_token for your client ID: signature, issuer, audience, and expiry. Returns the claims, or null.

verifyAveIdTokenFromAuthHeader(authorization, options)

Reads a Bearer token from an Authorization header value and verifies it with verifyAveIdToken. Returns { subject, claims }, where subject is the identity ID, or null.
getBearerToken(authorization) returns the raw token from a header value without verifying it.

exchangeCodeServer(config, payload)

Same as exchangeCode but uses clientSecret instead of codeVerifier. Use this for confidential clients (server-side token exchange).

refreshTokenServer(config, payload)

Refreshes tokens using client secret (confidential client).

TokenResponse

The shape returned by exchangeCode, exchangeCodeServer, refreshToken, refreshTokenServer, and finishPkceLogin. The token endpoint never returns key fields; finishPkceLogin, completeOAuthCallback, and mergeAppEncryptionFromUrl add them from the callback fragment.

Error handling

All SDK functions throw an Error when the server returns a non-2xx response. The error message is the error field from the response body (e.g. "invalid_grant", "invalid_client").

See also

  • App encryption — reading keys from the callback, public-key lookup, and encrypting for other users.
  • Ave Session — persisting tokens and keys, and refreshing before expiry.
Last modified on September 28, 2026