Skip to content

RewriteToday/node

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Rewrite Node SDK

@rewritejs/sdk, the official Node.js/TypeScript SDK for the Rewrite API.

It wraps authentication, typed REST calls, and resource helpers on top of @rewritejs/rest and @rewritejs/types.

Installation

Use your preferred package manager:

bun add @rewritejs/sdk
# Or
npm install @rewritejs/sdk
# Or
pnpm add @rewritejs/sdk
# Or
yarn add @rewritejs/sdk

Quick Start

import { Rewrite } from '@rewritejs/sdk';

const rewrite = new Rewrite(process.env.REWRITE_API_KEY!);

const project = await rewrite.projects.get('123456789012345678');

console.log({ project });

Create The Client

You can pass the API key directly or use the full options object.

import { Rewrite } from '@rewritejs/sdk';

const advanced = new Rewrite({
	secret: 'rw_live_xxx',
	rest: {
		timeout: 10_000,
		headers: {
			'x-trace-id': 'my-service',
		},
		retry: {
			max: 3,
			delay: (attempt) => attempt * 250,
		},
	},
});

Projects

const project = await rewrite.projects.create({ name: 'AbacatePay Notifications' });

console.log({ project });

Templates

const project = '123456789012345678';

const created = await rewrite.templates.create({
	project,
	name: 'welcome_sms',
	content: 'Hey {{name}}, welcome to {{company}}!',
	variables: [
		{ name: 'name', fallback: 'customer' },
		{ name: 'company', fallback: 'Rewrite' },
	],
});

const templates = await rewrite.templates.list(project, { limit: 20 });

console.log({ templates });

Webhooks

import { WebhookEventType, WebhookStatus } from '@rewritejs/types';

const project = '123456789012345678';

const hook = await rewrite.webhooks.create({
	project,
	name: 'delivery-events',
	endpoint: 'https://example.com/rewrite/webhooks',
	events: [WebhookEventType.SMSDelivered, WebhookEventType.SMSFailed],
});

await rewrite.webhooks.update(hook.data.id, {
	project,
	status: WebhookStatus.Inactive,
});

const hooks = await rewrite.webhooks.list(project, { limit: 10 });

console.log({ hooks });

API Keys

import { APIKeyScope } from '@rewritejs/types';

const project = '123456789012345678';

const key = await rewrite.apiKeys.create({
	project,
	name: 'backend-prod',
	scopes: [APIKeyScope.ReadProject, APIKeyScope.ReadTemplates],
});

console.log({ key });

Error Handling

Requests run through @rewritejs/rest. HTTP failures can throw HTTPError.

import { HTTPError } from '@rewritejs/rest';

try {
	await rewrite.projects.get('invalid_id');
} catch (error) {
	if (error instanceof HTTPError) {
		console.error('HTTP Error:', error.status, error.method, error.url);
	}
}

Made with 🤍 by the Rewrite team.
SMS the way it should be.

About

Rewrite's official Node.js SDK

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project