> For the complete documentation index, see [llms.txt](https://docs.northstar.sonicsvm.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.northstar.sonicsvm.org/getting-started/hello-world.md).

# Hello World

The shortest path from zero to a transaction running inside a NorthStar session. Five steps; \~30 lines of code.

## What we're going to build

Open a session, transfer SOL between two accounts inside the ER, settle back. Nothing fancy — but every step that matters is in there: session lifecycle, account delegation, ER-side execution, atomic settle-back.

## Setup

```typescript
import { NorthStarSDK } from "@sonicsvm/northstar-sdk";
import {
  Connection,
  Keypair,
  PublicKey,
  SystemProgram,
  Transaction,
} from "@solana/web3.js";

const PORTAL = new PublicKey("74iiMCqFw1afWyp3tdh9pUqfRfCRq7gfdC2YZoNGpovt");
const L1_RPC = new Connection("https://api.devnet.sonic.game", "confirmed");
const ER_RPC = new Connection("https://ephemeral.devnet.sonic.game", "confirmed");

const sdk = new NorthStarSDK({
  portalProgramId: PORTAL,
  customEndpoints: { solana: L1_RPC, ephemeralRollup: ER_RPC },
});

const owner = Keypair.generate();
const recipient = Keypair.generate();

// Fund the owner — devnet faucet, ~5 SOL is comfortable for the session.
// In your dev workflow, prefer the deploy payer at ~/.config/solana/id.json.
```

## Step 1: Open a session

```typescript
const gridId = 1n;
const ttlSlots = 2_000n;       // ~ 13 minutes at 400ms slots
const feeCap = 10_000_000n;     // 0.01 SOL fee budget

const { sessionAddress } = await sdk.openSession({
  owner,
  gridId,
  ttlSlots,
  feeCap,
});

console.log(`Session: ${sessionAddress.toBase58()}`);
```

The Portal program creates a session PDA derived from `(portal, owner, gridId)`. From here, anything you delegate to this session is locked on L1 and writable on the ER.

## Step 2: Delegate the recipient account

```typescript
await sdk.delegate({
  owner,
  gridId,
  account: recipient.publicKey,
  ownerProgram: SystemProgram.programId,
});
```

The Portal records that `recipient` is now bound to this session. After this returns, the account is read-only on L1 and writable inside the ER.

## Step 3: Run the transaction inside the ER

```typescript
const transferIx = SystemProgram.transfer({
  fromPubkey: owner.publicKey,
  toPubkey: recipient.publicKey,
  lamports: 1_000_000,            // 0.001 SOL
});

const tx = new Transaction().add(transferIx);
tx.feePayer = owner.publicKey;
tx.recentBlockhash = (await ER_RPC.getLatestBlockhash()).blockhash;
tx.sign(owner);

const sig = await ER_RPC.sendRawTransaction(tx.serialize());
await ER_RPC.confirmTransaction(sig, "confirmed");
console.log(`ER tx confirmed: ${sig}`);
```

Same `SystemProgram.transfer` instruction you'd send to L1 — different RPC. The ER processes it locally; confirmation lands in milliseconds (see [Real-time confirmation](https://github.com/mirrorworld-universe/northstar-docs/blob/main/architecture/real-time/README.md)).

## Step 4: Settle back

```typescript
await sdk.closeSession({ owner, gridId });
console.log(`Session closed; recipient balance is now visible on L1.`);
```

`CloseSession` undelegates every account bound to this session in one atomic step. Recipient's new balance shows up on L1.

## Step 5: Verify

```typescript
const balance = await L1_RPC.getBalance(recipient.publicKey);
console.log(`Recipient L1 balance: ${balance} lamports`);
// Expected: 1_000_000 (the transfer landed, settle-back committed it)
```

## What just happened

```mermaid
sequenceDiagram
    participant Owner
    participant Portal
    participant ER as ER session
    participant L1

    Owner->>Portal: open
    Owner->>Portal: delegate
    Portal->>ER: account marked writable
    Owner->>ER: transfer
    Owner->>Portal: close
    ER-->>Portal: atomic settle
    Portal->>L1: recipient updated
```

Five operations, two RPCs, one `Transfer` ix that runs identically to how it would on L1.

## Where to go from here

* [Recipes](https://github.com/mirrorworld-universe/northstar-docs/blob/main/getting-started/recipes/README.md) — patterns for agents, oracles, orderbooks.
* [Concepts: sessions](https://github.com/mirrorworld-universe/northstar-docs/blob/main/concepts/sessions/README.md) — the formal model.
* [Migrate to NorthStar](https://github.com/mirrorworld-universe/northstar-docs/blob/main/build/migrate/README.md) — for an existing Solana program.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.northstar.sonicsvm.org/getting-started/hello-world.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
