> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tortus.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# How it works

> The mental model behind the embedded TORTUS client.

A few concepts explain almost everything about how the SDK behaves. Once these click, the rest of the API follows naturally.

## The embedded model

The SDK renders TORTUS inside a **sandboxed iframe** in a container you provide. Your application and TORTUS communicate over a secure message channel: you send instructions (start a consultation, navigate a screen) and TORTUS sends events back (started, completed, error).

Your users never leave your application, and patient data is handled inside the protected TORTUS experience.

## Standby mode

The client **always starts in standby mode** after `loadTortus()` resolves. The standby screen waits for instructions until you start a consultation or navigate elsewhere.

```ts theme={null}
const client = await loadTortus({
  /* ... */
});
// The client is now showing the standby screen, ready for instructions.
```

You can always return to it with `client.display('standby')`.

## Event-driven architecture

All consultation and meeting handling is **event-based**. You subscribe with `client.on(...)` and react to lifecycle events such as `consultation:started`, `consultation:completed`, and `consultation:error`. Each event carries a `reference` so you can correlate it with your own records.

Completion events hand you a `finish()` callback that you **must call** to acknowledge receipt of the results.

```ts theme={null}
client.on('consultation:completed', async ({ consultation, result, finish }) => {
  await handleResult(result);
  await finish({ status: 'success' });
});
```

Learn the full lifecycle in [Handling events & results](/guides/events-and-results).

## Screens and navigation

You can move the embedded experience between screens programmatically:

```ts theme={null}
await client.display('home'); // the TORTUS home screen
await client.display('standby'); // back to standby
```

Navigation returns a promise that **rejects if the operation isn't possible** (for example, navigating away mid-consultation).

## Client lifecycle

The client owns the iframe and all its resources. When you're done, for example when unmounting the component that hosts TORTUS, call `destroy()` to clean everything up.

```ts theme={null}
await client.destroy();
```

<Note>
  `destroy()` releases all resources but does **not** remove historical data saved in the browser.
  Individual consultations and meetings also expose `close()` for finer-grained teardown.
</Note>

## Putting it together

<CardGroup cols={2}>
  <Card title="Authentication" icon="key" href="/authentication">
    How sessions are secured with launch tokens.
  </Card>

  <Card title="Consultations" icon="stethoscope" href="/guides/consultations">
    Start consultations across every mode.
  </Card>

  <Card title="Events & results" icon="bell" href="/guides/events-and-results">
    Subscribe to the lifecycle and read artifacts.
  </Card>

  <Card title="API reference" icon="code" href="/reference/api">
    The full method surface.
  </Card>
</CardGroup>
