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

# Installation

> Install the type-safe lettr package for Node.js and TypeScript, create a client, and handle the result-based data and error pattern

The official `lettr` package is a type-safe API client for Node.js and TypeScript. It covers sending, templates, domains, webhooks, audience, and campaigns — with no thrown exceptions: every method returns a `Result` you destructure into `data` and `error`.

## Requirements

* Node.js 18 or higher
* A [verified sending domain](/learn/domains/sending-domains) and an [API key](/learn/api-keys/introduction)

## Install the Package

```bash theme={null}
npm install lettr
# or: pnpm add lettr / yarn add lettr / bun add lettr
```

## Create a Client

```typescript theme={null}
import { Lettr } from "lettr";

const client = new Lettr(process.env.LETTR_API_KEY!);
```

<Tip>
  Store your API key in an environment variable, never in source. API keys use the `lttr_` prefix followed by 64 hexadecimal characters.
</Tip>

## The Result Pattern

Every SDK method returns a `Result<T>` — a discriminated union of `{ data, error }`. The SDK never throws for API or network failures; you check `error` instead:

```typescript theme={null}
const { data, error } = await client.emails.send({
  from: "you@yourdomain.com",
  to: ["recipient@example.com"],
  subject: "Hello",
  html: "<p>Hi there</p>",
});

if (error) {
  // error.type is "validation" | "api" | "network"
  console.error(error.type, error.message);
  return;
}

console.log(data.request_id); // narrowed to non-null here
```

Because `data` and `error` are discriminated, TypeScript narrows `data` to non-null only after you've handled `error`. See [Sending Emails → Error Handling](/quickstart/nodejs/sending-emails#error-handling) for the full error model.

## Verify Your Setup

```typescript theme={null}
const { data, error } = await client.authCheck();

if (error) {
  console.error("API key invalid:", error.message);
} else {
  console.log("Authenticated, team:", data.team_id);
}
```

## Available Resources

```typescript theme={null}
client.emails;     // send, schedule, list, get
client.templates;  // manage Lettr templates
client.domains;    // manage sending domains
client.webhooks;   // manage webhook endpoints
client.audience;   // lists, contacts, topics, properties, segments
client.campaigns;  // list, send, and schedule campaigns
client.projects;   // list projects
```

## What's Next

<CardGroup cols={2}>
  <Card title="Sending Emails" icon="paper-plane" href="/quickstart/nodejs/sending-emails">
    Send HTML, text, and template emails
  </Card>

  <Card title="Next.js" icon="react" href="/quickstart/nodejs/nextjs">
    Use the SDK in Next.js
  </Card>
</CardGroup>
