Client SDK

Web SDK and Sign in with Wavelike

Add a Sign in with Wavelike button to a web app, keep the person's token fresh across tabs, and call the Wavelike API as them from the browser.

What it does

@wavelike/web is for web apps your coworkers sign in to with their company account.

  • A <wavelike-sign-in> button opens a popup, sends the person through your company sign-in, and closes the popup when they're done. It then shows who is signed in, with a Sign out item.
  • The client keeps the person's Wavelike token fresh. Every tab of your site shares one sign-in, and only one tab refreshes at a time, so the rotating refresh token is never sent twice.
  • The same client calls the Wavelike API as the signed-in person: Responses (plain and streamed), files, models, and their usage. Usage limits, logs and billing apply to that person.
  • No secret ships to the browser. The button uses a public Connect client with PKCE.

Set up

  1. 1Create the app in Organization settings > Apps for coworkers (or with wavelike app create "Name" --audience workforce). That gives you a sign-in client ID that starts with wkr_client_. For an app people sign in to through Connected apps, choose your organization's company sign-in and the public client type.
  2. 2Add https://<your site>/wavelike/callback to the app's redirect URIs. Use http://localhost:<port>/wavelike/callback while you develop.
  3. 3Serve a page at /wavelike/callback that loads the callback script. It finishes the sign-in and closes the popup.
  4. 4Add the button where people should sign in. The app's page in the dashboard shows this code with your IDs filled in under Code.
Button
html
1<script type="module" src="https://cdn.jsdelivr.net/npm/@wavelike/web@0/dist/web/src/button.js"></script>2<wavelike-sign-in app-id="<app-id>" client-id="wkr_client_..."></wavelike-sign-in>
/wavelike/callback
html
1<!doctype html>2<title>Signing in</title>3<p data-wavelike-callback>Signing in…</p>4<script type="module" src="https://cdn.jsdelivr.net/npm/@wavelike/web@0/dist/web/src/callback.js"></script>

Call the API as the signed-in person

getWavelike returns the same client the button uses, so your code sees the same sign-in.

  • Every request gets a fresh token first. A request that comes back 401 refreshes once and retries.
  • Errors are typed: SignInRequiredError (the sign-in ended), AccessDeniedError, UsageLimitError (with the limit, what was used, and when it resets), RateLimitError, NetworkError and APIError.
  • Pass labels and threadId in a request's options to tag usage, as with the HTTP headers.
JavaScript
javascript
1import "@wavelike/web/button";2import { getWavelike } from "@wavelike/web/button";34const wavelike = getWavelike({ appId: "<app-id>", clientId: "wkr_client_..." });56document.querySelector("wavelike-sign-in").addEventListener("signedin", async (event) => {7  console.log("Hello", event.detail.user.name);89  const reply = await wavelike.responses.create({ model: "<model>", input: "Summarize my week" });10  console.log(reply.output_text);1112  for await (const text of wavelike.responses.stream({ model: "<model>", input: "Write a haiku" }).text()) {13    output.textContent += text;14  }15});

Button options

  • app-id and client-id are required. redirect-uri defaults to <your origin>/wavelike/callback.
  • domain picks the company sign-in when your organization has more than one. With one, leave it out. access-profile-id asks for one access profile.
  • mode="redirect" signs in in the same tab instead of a popup. A popup the browser blocks falls back to a redirect on its own.
  • storage is indexeddb (the default: kept across reloads and shared by every tab), session (this tab only, gone when it closes) or memory (gone on reload).
  • label changes the text. theme (light or dark; it follows the system by default), size (small or large), shape (pill or square) and hide-when-signed-in change the look. Style it further with the button, label and menu CSS parts.
  • Events: signedin (detail.user, and detail.restored when the sign-in was kept from an earlier visit), signedout, and error (detail.error, a SignInError whose code is popup_closed, access_denied, timeout and so on).

React

Call signIn from a click handler so the browser allows the popup. useWavelike also finishes a redirect sign-in when it renders on the callback page.

React
tsx
1import { createWavelike } from "@wavelike/web";2import { WavelikeProvider, useWavelike } from "@wavelike/web/react";34const wavelike = createWavelike({ appId: "<app-id>", clientId: "wkr_client_..." });56export function App() {7  return (8    <WavelikeProvider client={wavelike}>9      <Account />10    </WavelikeProvider>11  );12}1314function Account() {15  const { user, loading, signIn, signOut } = useWavelike();16  if (loading) return null;17  return user18    ? <button onClick={signOut}>Sign out {user.name}</button>19    : <button onClick={() => signIn()}>Sign in with Wavelike</button>;20}

How sign-in works

  • The popup goes to /connect/authorize with a PKCE challenge, through your company sign-in, and back to /wavelike/callback with a code. The callback page hands the code to the tab that opened it over a same-origin BroadcastChannel, because the sign-in pages cut the popup off from window.opener.
  • The opening tab exchanges the code at /connect/token and loads the person from /connect/userinfo. Tokens last 15 minutes and are refreshed a minute early. Refresh tokens rotate on every use and last 30 days.
  • Browsers may only use a public client's tokens from the origin of one of its redirect URIs. A request from any other origin to /connect/token or /connect/revoke is refused with unauthorized_client.
  • A refresh is recorded before it is sent. If the tab closes before the answer arrives, the next refresh sends the same refresh_request_id, which the server replays instead of treating as a stolen token.
  • Sign out clears the sign-in in every tab and revokes it on Wavelike, so the refresh token stops working everywhere.
  • Anything that can run script on your site can read the stored token, as with any browser sign-in. Keep a strict Content Security Policy, or choose storage="memory" for the most sensitive tools.