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

# TypeScript SDK

> Use the official @crevio/sdk to interact with the Crevio API

The `@crevio/sdk` package provides a type-safe TypeScript client for the Crevio API. It works in Node.js, browsers, Cloudflare Workers, and Deno.

## Installation

<CodeGroup>
  ```bash npm theme={null}
  npm install @crevio/sdk
  ```

  ```bash bun theme={null}
  bun add @crevio/sdk
  ```

  ```bash pnpm theme={null}
  pnpm add @crevio/sdk
  ```
</CodeGroup>

## Quick start

```typescript theme={null}
import { Crevio } from "@crevio/sdk";

const crevio = new Crevio({
  apiKeyAuth: "YOUR_API_TOKEN",
});
```

## Usage examples

### List products

```typescript theme={null}
const products = await crevio.products.list();

for (const product of products) {
  console.log(product.name, product.id);
}
```

### Get a product

Retrieve by ID or slug:

```typescript theme={null}
// By ID
const product = await crevio.products.get({ id: "prod_abc123" });

// By slug
const product = await crevio.products.get({ id: "my-course" });
```

### Get a product with reviews expanded

```typescript theme={null}
const product = await crevio.products.get({
  id: "my-course",
  expand: "reviews",
});

for (const review of product.reviews ?? []) {
  console.log(review.rating, review.customerName, review.content);
}
```

### Create a checkout

```typescript theme={null}
const checkout = await crevio.checkouts.create({
  email: "customer@example.com",
  lineItems: [
    {
      priceVariant: "pv_xyz789",
      quantity: 1,
    },
  ],
});

console.log(checkout.clientSecret); // Use with Stripe.js
```

### List orders with expanded data

```typescript theme={null}
const orders = await crevio.orders.list({
  expand: "line_items,customer",
  limit: 50,
});
```

### Create a customer

```typescript theme={null}
const customer = await crevio.customers.create({
  email: "customer@example.com",
  firstName: "Jane",
  lastName: "Doe",
});
```

### Create a discount

```typescript theme={null}
const discount = await crevio.discounts.create({
  code: "SUMMER20",
  discountType: "percent_off",
  percentOff: 20,
  duration: "once",
  maxRedemptions: 100,
});
```

### Manage webhook endpoints

```typescript theme={null}
// Create a webhook endpoint
const webhook = await crevio.webhookEndpoints.create({
  url: "https://example.com/webhooks/crevio",
  enabledEvents: ["order.paid", "checkout.created"],
});

// Save the secret for signature verification
console.log(webhook.secret);

// List available event types
const events = await crevio.webhookEndpoints.listEvents();
```

### Pay an invoice

Create a checkout to pay an existing invoice:

```typescript theme={null}
const checkout = await crevio.checkouts.create({
  invoice: "inv_abc123",
});

// Redirect customer to checkout.url
console.log(checkout.url);
```

### List refunds

```typescript theme={null}
// List all refunds
const refunds = await crevio.refunds.list();

// List refunds for a specific order
const orderRefunds = await crevio.refunds.list({
  order: "ord_abc123",
});
```

### Manage files

```typescript theme={null}
// Upload a file via external URL
const file = await crevio.files.create({
  sourceType: "external",
  url: "https://example.com/image.png",
});

// Create a direct upload
const upload = await crevio.files.create({
  sourceType: "upload",
  filename: "document.pdf",
});
// Use upload.uploadUrl and upload.uploadHeaders to PUT the file,
// then confirm: await crevio.files.uploaded({ id: upload.id });
```

### Send an email

```typescript theme={null}
const email = await crevio.emails.create({
  subject: "Your purchase is ready",
  html: "<h1>Thank you!</h1><p>Your download is ready.</p>",
  to: ["customer@example.com"],
});
```

### List webhook events

```typescript theme={null}
// List recent webhook events
const events = await crevio.webhookEvents.list();

// Filter by event type
const orderEvents = await crevio.webhookEvents.list({
  type: "order.paid",
});
```

## Error handling

The SDK throws typed errors that you can catch:

```typescript theme={null}
try {
  const product = await crevio.products.get({
    id: "prod_nonexistent",
  });
} catch (error) {
  if (error.statusCode === 404) {
    console.log("Product not found");
  }
}
```

## Runtime support

| Runtime            | Supported |
| ------------------ | --------- |
| Node.js 18+        | Yes       |
| Browsers           | Yes       |
| Cloudflare Workers | Yes       |
| Deno               | Yes       |
| Bun                | Yes       |

<Note>
  The SDK is generated with [Speakeasy](https://speakeasy.com) and includes Zod validation, automatic retries, and full TypeScript type definitions.
</Note>
