Client SDK

Server SDK

Sign in your own users, check who is calling, run Service Connections, and give the browser Wavelike tokens from your backend.

What it does

@wavelike/server runs on Node 20 and later, Bun, Deno and Cloudflare Workers, with no runtime dependencies.

  • Partner assertion: your server vouches for a user it already signed in, and gets Wavelike tokens for them in one call.
  • verify(request) says which person a Wavelike access token belongs to, for your own API.
  • Service Connections: a backend acting as itself, with refresh tokens rotated and journaled safely.
  • Handlers are Web-standard Request to Response functions, so they work as Next.js route handlers and in Hono, Workers, Remix, SvelteKit, Bun.serve and Deno.serve.
Set up
javascript
1import { createWavelikeServer } from "@wavelike/server";23const wavelike = createWavelikeServer({4  appId: "<app-id>",5  clientId: "wkr_client_...",6  clientSecret: process.env.WAVELIKE_CLIENT_SECRET, // confidential clients only7});

Sign in your own users

Create a connected app that signs people in with the app's own accounts (partner assertion) and the confidential client type, and keep its secret on your server. Then call signInUser after your own sign-in. Usage, limits and logs are attributed to that user.

signInUser calls POST /connect/sessions and swaps the session for tokens on /connect/token with grant_type=urn:wavelike:params:oauth:grant-type:partner-session, so no browser is involved. The session is single-use and lasts five minutes.

Refresh tokens rotate on every use. The SDK derives each refresh_request_id from the refresh token, so a retry from any process is replayed instead of being treated as a stolen token. Save the new tokens in onTokens.

Partner assertion
javascript
1const tokens = await wavelike.connect.signInUser({2  id: "user_123", email: "ada@example.com", name: "Ada",3  allowedStackIds: ["support-chat"], // optional, capped by the client4});56const reply = await wavelike7  .as(tokens, { onTokens: (next) => saveTokens("user_123", next) })8  .responses.create({ model: "<model>", input: "Hi" });910await wavelike.connect.revoke(tokens.refreshToken);

Give the browser a token

With the Web SDK, the browser can ask your backend for a token instead of signing in itself. tokenHandler returns an access token for the user your own session says is signed in, and keeps the refresh token on the server. It refreshes near expiry and signs the user in again if the sign-in ended.

Tokens are kept per user in memory by default. With several instances or serverless, pass a store with get(userId) and set(userId, tokens).

Server route
javascript
1import { tokenHandler } from "@wavelike/server/handlers";23export const GET = tokenHandler({4  server: wavelike,5  getUser: async (request) => {6    const me = await mySession(request);7    return me ? { id: me.id, email: me.email, name: me.name } : null;8  },9});
Browser
javascript
1import { createWavelike } from "@wavelike/web";23const wavelike = createWavelike({4  appId: "<app-id>",5  getToken: () => fetch("/api/wavelike-token").then((r) => r.json()),6});

Check who is calling

verify(request) reads Authorization: Bearer from the request and asks /connect/userinfo whose token it is. It throws SignInRequiredError (401) when there is no token, Wavelike rejects it, or the token belongs to another app or Connect client. Tokens from clientId are accepted; list your app's other clients (such as the browser's public client) in acceptClientIds. Answers are cached for up to a minute, never past the token's expiry. emailVerified is true only for company sign-in.

Your API
javascript
1const caller = await wavelike.verify(request);2// { id, email, emailVerified, name, identity, appId, organization, accessProfileId }

Sign-in held on your server

For apps that want no Wavelike tokens in the browser at all. sessionHandlers runs company sign-in with PKCE from your server and keeps the tokens in an encrypted (AES-GCM), HttpOnly, SameSite=Lax cookie. Register <your origin>/wavelike/callback as a redirect URI.

getSession refreshes a session near expiry. When it does, set the cookie it returns on your response. Two requests refreshing the same cookie at once send the same request ID, so Wavelike replays the answer instead of ending the sign-in.

Session routes
javascript
1import { sessionHandlers } from "@wavelike/server/handlers";23const auth = sessionHandlers({ server: wavelike, secret: process.env.SESSION_SECRET });4// GET  /wavelike/sign-in   -> auth.signIn   (?return_to=/path)5// GET  /wavelike/callback  -> auth.callback6// POST /wavelike/sign-out  -> auth.signOut78const session = await auth.getSession(request);9if (session) {10  const reply = await session.client.responses.create({ model: "<model>", input: "Hi" });11  const cookie = await session.cookie(); // Set-Cookie when not null12}

Service Connections

serviceConnections.authorizationRequest starts an install and returns the approval URL, with the state and PKCE verifier to keep for your callback. exchangeCode finishes it. serviceConnection({ store }) then calls the API as the connection.

The store is get(), set(credentials) and an optional lock(run). Encrypt it at rest, and with more than one process give it a durable lock. Each refresh is saved before it's sent and resent with the same request ID after a lost answer. When Wavelike refuses a refresh, or a replay can't be recovered, the connection stops with a ServiceConnectionError until an operator reauthorizes it.

Service Connection
javascript
1const install = await wavelike.serviceConnections.authorizationRequest({2  redirectUri: "https://jobs.example.com/wavelike/installed",3  connectionName: "Nightly summaries",4  environment: "production",5  deploymentInstance: "worker-1",6  stackIds: ["nightly"],7});8// Send an operator to install.url. At the callback, after checking state:9await store.set(await wavelike.serviceConnections.exchangeCode({ code, redirectUri, codeVerifier }));1011const service = wavelike.serviceConnection({ store });12await service.responses.create({ model: "nightly", input });

Rate limits

/connect/sessions, /connect/token and /connect/revoke allow 20 requests a minute per IP address. Requests that send a client secret are counted separately, per client and IP address, at 600 a minute, so a busy server signing many users in isn't throttled by its own address.