Guide 03 · Browser SDK

Browser SDK (/sdk.js) & Declarative Form Autofill

Add Sign in with Protext to any web page using public/sdk.js: zero-secret PKCE S256 in the browser, data-protext form autofill, and protext:signed-in events.

README.mdpublic/sdk.jsdocs/RELYING-PARTIES.mdSPEC.md §6.1
Quick answer · PROTEXT/0.1

The zero-dependency browser SDK at https://protext.org/sdk.js turns two HTML attributes—data-protext-signin and data-protext="<kind>"—into a complete OAuth 2.1 PKCE sign-in and form-autofill flow with no client secret in the browser.

On click, sdk.js generates a 48-byte random PKCE verifier and 16-byte state in sessionStorage, hashes the verifier with Web Crypto SHA-256, redirects to /oidc/authorize, exchanges the returned code at /oidc/token, cleans the URL query string, autofills matching form fields, and dispatches protext:signed-in.

Declarative attributes and JavaScript API exposed by https://protext.org/sdk.js

Attribute / MethodRoleBehavior in public/sdk.js
data-client-idScript tag configEnables declarative mode using your registered public px_* client_id
data-scopeScript tag configRequested scopes (defaults to "openid profile email")
data-redirect-uriScript tag configOptional override; defaults to current page URL without query/hash
data-issuerScript tag configOptional custom Protext provider origin (defaults to script origin)
data-protext-signinButton attributeWires click -> Protext.signIn(cfg); injects default label if element is empty
data-protext="<kind>"Input/textarea/img/textFilled by Protext.fill(bundle): sets value + fires input/change, img.src, or textContent; joins multi-value kinds with \n in <textarea>
Protext.signIn(opts) / handleCallback(opts)Programmatic JS APIManual PKCE start and callback exchange (returns null when no ?code is present)
Protext.get / all / fill / contextBundle helpersRead first or all stringified values of a kind, fill DOM nodes, or re-fetch /api/v0/context
Wire format & code example

Declarative two-tag browser integration and programmatic callback pattern

Matches app/developers/page.tsx, public/sdk.js, and docs/RELYING-PARTIES.md verbatim.

<script src="https://protext.org/sdk.js"
  data-client-id="px_YOUR_CLIENT_ID"
  data-scope="openid profile email context:work"></script>

<button data-protext-signin></button>
<input name="name" data-protext="identity.name">
<input name="email" data-protext="contact.email">
<textarea name="building" data-protext="work.building"></textarea>

<script>
document.addEventListener("protext:signed-in", (e) => {
  // e.detail = { token_type, access_token, id_token, context, context_token, ... }
  console.log("Items shared:", e.detail.context.items);
});
document.addEventListener("protext:error", (e) => {
  console.error("Protext sign-in error:", e.detail);
});
</script>

How declarative autofill updates React, Vue, and vanilla forms

When Protext.fill(bundle, root) finds an element with data-protext="<kind>", it extracts all matching items from bundle.items via Protext.all(bundle, kind). For <textarea> elements with multiple matching items (such as work.building or work.looking-for), values are joined with newlines; for single-line inputs, the first value is assigned.

After assigning el.value, sdk.js dispatches bubbling input and change events and stamps data-protext-filled="1" on each filled element so form listeners and visual highlights react immediately. For <img> elements it sets el.src, and for non-input elements it assigns el.textContent.

Dedicated callback route pattern (used by America.co and TeamMeet)

If your application uses a dedicated callback path like /protext/callback instead of returning to the form URL directly, load /sdk.js without data-client-id (or pass redirectUri explicitly) and call Protext.signIn({ clientId, scope, redirectUri }) on button click.

On /protext/callback, call const result = await Protext.handleCallback({ clientId }), persist result.context in sessionStorage or post it to your server action, and redirect back to the onboarding screen. Protext.handleCallback automatically strips ?code and ?state from the browser address bar with history.replaceState.

  • sdk.js is served with Access-Control-Allow-Origin: * and Cache-Control: public, max-age=300.
  • No client secret is ever placed in browser code; public clients rely on PKCE S256 and exact redirect URI matching.
  • Multi-provider apps can pass data-issuer on the script tag to authenticate against any self-hosted PROTEXT/0.1 provider.
Next step

Verify against the normative specification or register your client.

Read PROTEXT/0.1 SpecDeveloper Quickstart10 Languages
FAQ

Frequently asked questions

Does public/sdk.js require a client secret?

No. The browser SDK is built for public OAuth 2.1 clients using PKCE (S256). Register your app with token_endpoint_auth_method set to none (the RFC 7591 default) so no secret is issued or exposed in client-side code.

How does /sdk.js handle URL cleanup after the OAuth redirect?

After exchanging ?code at POST /oidc/token (or if an error parameter is returned), handleCallback calls history.replaceState to remove the query string and hash from the current URL and clears the temporary sessionStorage key protext:pkce.

What happens if a user unticks a field that matches a data-protext input?

Protext.fill() skips any element whose requested kind has no items in the consented bundle, leaving that input untouched so the person can type manually or leave it blank.

How are multi-valued kinds like work.building filled into a form?

If the target element is a <textarea> and multiple items match the kind, sdk.js joins them with newline characters. For single-line <input> elements, it inserts the first matching item.

Can I use /sdk.js with a self-hosted Protext provider?

Yes. Set data-issuer="https://your-provider.example" on the <script> tag, or serve sdk.js from your own provider origin; the SDK defaults its issuer to the script tag's origin when data-issuer is omitted.

Where can I test the browser SDK live?

Visit https://protext.org/demo, which runs a registered demo public client (px_demo) using /sdk.js to demonstrate consent and automatic form filling.

Related guides

Continue exploring PROTEXT/0.1

Guide 04 · RFC 7591 Registration

RFC 7591 Dynamic Client Registration

Register public PKCE or confidential OIDC relying parties immediately at POST https://protext.org/oidc/register using RFC 7591 without creating a provider account first.

Read guide →
Guide 01 · OIDC & OAuth 2.1

OpenID Connect & OAuth 2.1 PKCE Integration

Integrate Sign in with Protext using OpenID Connect Core discovery, OAuth 2.1 authorization code flow with S256 PKCE, RFC 9207 iss checks, and RS256 ID tokens.

Read guide →
Guide 02 · Data Model & Scopes

Context Bundles, Categories & Scopes Reference

Complete PROTEXT/0.1 reference for the 7 context categories, 23 well-known item kinds, OIDC claim mappings, structured values, and bundle envelope fields.

Read guide →