---
title: Full example
description: Run a session and receive its reply with the Beeuni SDK.
---

Save the script as `full-example.ts`. It creates a session in an existing project, opens its event stream, sends a prompt, and reads the transcript.

```ts
import { createBeeuni, narrowChatEvent } from '@beeuni/sdk';

async function main() {
  const apiKey = process.env.BEEUNI_API_KEY;
  const projectId = process.env.BEEUNI_PROJECT_ID;
  if (!apiKey || !projectId) {
    throw new Error('Set BEEUNI_API_KEY and BEEUNI_PROJECT_ID.');
  }

  const beeuni = createBeeuni({
    backendUrl: 'https://api.beeuni.ai/v1',
    getToken: async () => apiKey,
  });
  const created = await beeuni.project(projectId).sessions.create({
    name: 'SDK example',
  });
  console.log('Session:', created.session_id);

  const session = beeuni.session(projectId, created.session_id);
  const { runtimeSessionId } = await session.ensureReady();
  let finish!: () => void;
  let failure: string | undefined;
  let submitted = false;
  const finished = new Promise<void>((resolve) => { finish = resolve; });

  const stream = await session.stream({
    onEvent: (event) => {
      const e = narrowChatEvent(event);
      if (!e || !submitted || !('sessionID' in e) || e.sessionID !== runtimeSessionId) return;
      if (e.type === 'message.part.updated') console.log(e.part);
      if (e.type === 'session.error') {
        failure = JSON.stringify(e.error ?? 'Session failed.');
        finish();
      }
      if (e.type === 'session.idle') finish();
    },
  });

  const timer = setTimeout(() => {
    failure = 'Reply timed out. Check the session for pending permissions or questions.';
    finish();
  }, 180_000);

  try {
    submitted = true;
    await session.send('Describe your capabilities in one paragraph.');
    await finished;
    if (failure) throw new Error(failure);
    console.log(JSON.stringify(await session.messages(), null, 2));
  } finally {
    clearTimeout(timer);
    stream.close();
  }
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});
```

## Run

Create an API key in **Account → Tokens**, and use a project the credential can access. The script uses the project's configured model; session usage consumes credits.

```bash
npm install @beeuni/sdk
BEEUNI_API_KEY=your-api-key BEEUNI_PROJECT_ID=your-project-id npx tsx full-example.ts
```

## Next steps

[Authentication](/docs/sdk/auth)

For a React interface, use [React hooks](/docs/sdk/react). For permissions, questions, and readiness errors, see [Sessions](/docs/sdk/sessions).
