@playspace-health/embed
Frame the real PlaySpace dashboard inside your application, signed in as one of your clinicians.
The clinician never sees a PlaySpace login. Your backend asks the Partner API for a one-time sign-in code for that clinician, and this package puts the code into an iframe that exchanges it for a PlaySpace session. Everything inside the frame is the same product, with the same permissions, that clinicians who sign in to PlaySpace directly use.
How it fits together
your browser page your backend PlaySpace
----------------- ------------ ---------
mount({ getCode }) --getCode--> POST /v1/partner/practitioners/{id}/sign-in-codes (machine token)
<--code----- { code, start_url, expires_at }
iframe src = /frame/start?token=<code> -----------------------------> session starts in the frame
...hours later, a session limit is hit...
frame: "session-code-needed" --getCode--> a fresh code --postMessage--> frame reconnects
- A code is single use and expires after 60 seconds. It is the only credential that reaches the browser, and it goes to exactly one place: the iframe
src, or onepostMessageaddressed to the PlaySpace origin. - Your Partner API credential stays on your server. The package never sees it.
- A framed session ends 24 hours after its sign-in code, and the frame asks your page for a new code shortly before that while the clinician is working. When the limit is hit anyway, it asks for a new code and carries on. After 1 hour with no activity the frame signs the clinician out and calls
onSignedOut; a live session, a recording or a running game counts as activity.
Install
npm install @playspace-health/embed
The package does nothing without a Partner API credential with the sessions:write scope. PlaySpace issues credentials at onboarding, after review.
Without npm
The package is one dependency-free ES module, so a server-rendered application in any language can load it from a CDN that mirrors npm:
<div id="playspace" style="height: 820px"></div>
<script type="module">
import { mount } from 'https://cdn.jsdelivr.net/npm/@playspace-health/embed@2.0.0/dist/index.js'
mount({
container: document.getElementById('playspace'),
ehrOrigin: 'https://agentic-ps.playspace.health',
// Your own server route: it calls the sign-in-codes endpoint and returns { code }.
getCode: ({ path }) =>
fetch('/playspace/sign-in-code', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ path }),
})
.then((response) => {
if (!response.ok) throw new Error(`sign-in code request failed: ${response.status}`)
return response.json()
})
.then((body) => body.code),
})
</script>
Keep the version in the URL. A URL without one follows the newest release, and an upgrade should be your decision.
Before the first frame: allow your origin
PlaySpace only renders inside pages served from origins you have listed, and refuses to mint a code until at least one is set. Set them once through the settings endpoint:
PATCH /v1/partner/settings
Authorization: Bearer <access token>
Idempotency-Key: <a new key>
Content-Type: application/json
{ "frame_origins": ["https://app.example.com"] }
Each origin is https, lower case, origin form only (no path, no trailing slash, no wildcard), at most 10. The list is replaced whole on every PATCH, so send every origin you still want.
Your backend: mint a sign-in code
One route that knows which clinician is signed in to your application, asks for a code for them, and returns only the code.
// POST /playspace/sign-in-code
export async function POST(request: Request) {
const practitionerId = await currentPlaySpacePractitionerId(request) // from your own session
const { path } = (await request.json().catch(() => ({}))) as { path?: string }
const response = await fetch(
`https://agentic-ps.playspace.health/v1/partner/practitioners/${practitionerId}/sign-in-codes`,
{
method: 'POST',
headers: {
authorization: `Bearer ${await partnerApiToken()}`, // your cached client-credentials token
'content-type': 'application/json',
},
body: JSON.stringify(path ? { path } : {}),
},
)
if (!response.ok) return new Response(await response.text(), { status: response.status })
const { data } = (await response.json()) as { data: { code: string } }
return Response.json({ code: data.code }, { headers: { 'cache-control': 'no-store' } })
}
pathis the first page inside PlaySpace, relative only (it starts with a single/). Without it the frame opens on/dashboard.- Decide which clinician from your own signed-in user, never from anything the browser sends.
404means the practitioner is not yours or was deleted.409 practitioner-unavailablemeans they are disabled or not onboarded yet.409 frame-origins-not-setmeans the settings step above has not been done.
Your page: mount the frame
import { mount } from '@playspace-health/embed'
const frame = mount({
container: document.getElementById('playspace')!,
ehrOrigin: 'https://agentic-ps.playspace.health',
getCode: ({ path }) => mySignInCodeRoute(path),
path: '/dashboard/clients',
onSignedOut: () => showSignedOutMessage(),
onFrameUnsupported: (reason) => console.info('PlaySpace frame unsupported:', reason),
})
| Option | Meaning |
|---|---|
container |
Element the iframe is appended to. Give it a height; the frame fills it. |
ehrOrigin |
The PlaySpace origin: the host you call the Partner API on, https://agentic-ps.playspace.health in production and https://agentic-ps-dev.playspace.health in development. Messages from any other origin are ignored. |
getCode |
Returns a fresh code from your backend. Called with { path } on mount and with {} on every reconnect, where the frame waits 10 seconds for the answer. |
path |
First page inside PlaySpace. Optional. |
onSignedOut |
The frame signed out, either through signOut() or because PlaySpace ended the session (for example the clinician was disabled). |
onFrameUnsupported |
cookie: the browser refused the framed session, and PlaySpace is offering to open in its own window. browser: PlaySpace is showing an unsupported browser warning. |
mount returns { iframe, signOut, destroy }:
await frame.signOut()signs the clinician out of PlaySpace and resolves once the frame confirms. Call it when your own user signs out.frame.destroy()removes the iframe and the message listener. Destroying does not sign out; callsignOut()first if the clinician is leaving.
Camera, microphone and screen share
The iframe is created with
allow="camera; microphone; display-capture; fullscreen; autoplay; clipboard-read; clipboard-write; picture-in-picture; screen-wake-lock"
and allowfullscreen, and no sandbox. Live sessions run inside the frame, and a cross-origin frame without these permissions fails with NotAllowedError before any prompt appears.
If your page, or a proxy in front of it, sends a Permissions-Policy header, that header must also allow each feature for the PlaySpace origin, or it wins over the iframe attribute:
Permissions-Policy: camera=(self "https://agentic-ps.playspace.health"), microphone=(self "https://agentic-ps.playspace.health"), display-capture=(self "https://agentic-ps.playspace.health"), fullscreen=(self "https://agentic-ps.playspace.health")
A page with no Permissions-Policy header needs no change.
Browsers
The frame is supported in Chrome, and in Safari 26.2 or later. Other browsers see a "Chrome is recommended" warning inside the frame (onFrameUnsupported('browser')). When a browser refuses the framed session cookie, PlaySpace offers to continue in its own window (onFrameUnsupported('cookie')).
Messages
You do not need to handle these yourself; the package does. They are listed so you can recognise them in your browser's tools.
- Frame to page:
{ "type": "playspace:session-code-needed", "nonce": "<random>" }. The package callsgetCode({})and answers with{ "type": "playspace:session-code", "nonce": "<same>", "code": "<code>" }, addressed toehrOrigin. - Frame to page:
{ "type": "playspace:signed-out" }. - Frame to page:
{ "type": "playspace:frame-unsupported", "reason": "cookie" | "browser" }.
A message is acted on only when it comes from ehrOrigin and from this iframe's own window.
Things that will bite you
- Keep the frame mounted. Remounting signs the clinician in again and throws away anything half done inside PlaySpace. Resize with CSS.
- Your origin must be listed exactly.
https://app.example.comandhttps://www.app.example.comare different origins. - Codes expire after 60 seconds and work once. Mint one per
getCodecall; never cache them. - Return only the code to the browser. The Partner API response also carries a
start_url; nothing in your page needs it.
License
Apache-2.0. See LICENSE and NOTICE.