Files
GibSend/apps/docs/get-started/nodejs.mdx
2025-05-17 14:13:32 +10:00

93 lines
1.9 KiB
Plaintext

---
title: NodeJS
description: "Send your mail using unsend in NodeJS"
icon: node-js
---
## Prerequisites
- [Unsend API key](https://app.unsend.dev/dev-settings/api-keys)
- [Verified domain](https://app.unsend.dev/domains)
## Using SDK
<Steps>
<Step title="Install SDK">
<CodeGroup>
```bash npm
npm install unsend
```
```bash yarn
yarn add unsend
```
```bash pnpm
pnpm add unsend
```
```bash bun
bun add unsend
```
</CodeGroup>
</Step>
<Step title="Initialize SDK">
Get the API key from the [Unsend dashboard](https://app.unsend.dev/dev-settings/api-keys) and initialize the SDK
```javascript
import { Unsend } from "unsend";
const unsend = new Unsend("us_12345");
```
If you are running a self-hosted version of Unsend, pass the base URL as the
second argument:
```javascript
const unsend = new Unsend("us_12345", "https://my-unsend-instance.com");
```
</Step>
<Step title="Send Email">
```javascript
unsend.emails.send({
to: "hello@acme.com",
from: "hello@company.com",
subject: "Unsend email",
html: "<p>Unsend is the best open source product to send emails</p>",
text: "Unsend is the best open source product to send emails",
});
```
</Step>
</Steps>
## Adding contacts programatically
<Steps>
<Step title="Get the contact book id">
Get the contact book id from the [Unsend dashboard](https://app.unsend.dev/contacts/). Copy the contact book id
</Step>
<Step title="Add contacts">
```javascript
unsend.contacts
.create("clzeydgeygff", {
email: "hey@koushik.dev",
firstName: "Koushik",
lastName: "KM",
})
```
</Step>
<Step title="Update contact">
```javascript
unsend.contacts.update("clzeydgeygff", contactId, {
firstName: "Koushik",
lastName: "KM",
});
```
</Step>
</Steps>