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

# TypeScript

> SDK for TypeScript/JavaScript

## Install

```bash theme={null}
npm install @toffeepay/sdk
```

## Configuration

```typescript theme={null}
import { Toffee } from "@toffeepay/sdk";

const toffee = new Toffee({
  accessToken: "your-access-token",
});
```

### Sandbox

```typescript theme={null}
const toffee = new Toffee({
  accessToken: "your-sandbox-token",
  environment: "sandbox",
});
```

## Checkout

Shorthand for creating a payment session:

```typescript theme={null}
const session = await toffee.checkout({
  item: { title: "Gem Pack 50", price: 100, currency: "USD" },
  gameId: "your-game-id",
  userId: "user-id",
  returnUrl: "https://example.com/return",
});

console.log(session.url); // redirect the user here
```

## Refund

Shorthand for creating a refund:

```typescript theme={null}
const refund = await toffee.refund({ paymentId: "pay_123" });
```

## Idempotency

Pass an idempotency key to protect against duplicate operations:

```typescript theme={null}
const session = await toffee.checkout(
  {
    item: { title: "Gem Pack 50", price: 100, currency: "USD" },
    gameId: "game_1",
    userId: "user_1",
    returnUrl: "https://example.com/return",
  },
  { idempotencyKey: "unique-key-123" },
);

const refund = await toffee.refund(
  { paymentId: "pay_123" },
  { idempotencyKey: "refund-key-456" },
);
```

## Resources

### Sessions

```typescript theme={null}
const { session } = await toffee.sessions.create({ ... });
const { session } = await toffee.sessions.get({ id: "sess_123" });
const { status }  = await toffee.sessions.status({ id: "sess_123" });
const { sessions } = await toffee.sessions.list({ gameId: "game_1" });
await toffee.sessions.cancel({ id: "sess_123" });
```

### Payments

```typescript theme={null}
const { payment } = await toffee.payments.get({ id: "pay_123" });
const { payments } = await toffee.payments.list({ gameId: "game_1" });
await toffee.payments.complete({ id: "pay_123" });
await toffee.payments.cancel({ id: "pay_123" });
```

### Refunds

```typescript theme={null}
const { refund } = await toffee.refunds.create({ paymentId: "pay_123" });
const { refund } = await toffee.refunds.get({ id: "ref_123" });
const { refunds } = await toffee.refunds.list({ paymentId: "pay_123" });
```

### Accounts

```typescript theme={null}
const { account } = await toffee.accounts.get({ id: "acc_123" });
const { accounts } = await toffee.accounts.list({ gameId: "game_1" });
```

### Deposits

```typescript theme={null}
const { deposit } = await toffee.deposits.get({ id: "dep_123" });
const { deposits } = await toffee.deposits.list({ gameId: "game_1" });
```
