ReSMS Logo
Skip to Content
QuickstartNext.js

Next.js

Welcome to the ReSMS Next.js integration guide.

This guide explains how you can quickly and securely send SMS messages using the ReSMS API in your Next.js applications.

Installation

First, install our official ReSMS NodeJS SDK:

npm install resms # or yarn add resms # or pnpm add resms

Setup

You need to get an API key on ReSMS Dashboard. Then import the package and create a new instance of the ReSMS class with your API key.

import { ReSMS } from "resms"; const resms = new ReSMS("re_12345");

Quickstart

Here are examples of how to use ReSMS in both App Router and Pages Router.

App Router

(app/api/send/route.ts)

import { ReSMS } from 'resms'; const sms = new ReSMS(process.env.RESMS_API_KEY!); export async function POST() { try { const result = await sms.send({ to: "+33123456789", text: "Your sign-in code is 123456", }); return Response.json(result); } catch (error) { console.error('Failed to send SMS:', error); return Response.json({ error }, { status: 500 }); } }

Pages Router

(pages/api/send.ts)

import type { NextApiRequest, NextApiResponse } from 'next'; import { ReSMS } from 'resms'; const sms = new ReSMS(process.env.RESMS_API_KEY!); export default async (req: NextApiRequest, res: NextApiResponse) => { if (req.method !== 'POST') return res.status(405).end(); try { const result = await sms.send({ to: "+33123456789", text: "Your sign-in code is 123456", }); return res.status(200).json(result); } catch (error) { console.error('Failed to send SMS:', error); return res.status(500).json({ error: 'SMS send failed' }); } };

Environment Variables

To keep it secure, we recommend using environment variables to store your API key.

RESMS_API_KEY=your-api-key-here

Then, in your code, you can access it like this:

const resms = new ReSMS(process.env.RESMS_API_KEY);

Error Handling

All API calls can throw errors. We recommend wrapping your SMS calls with try-catch:

try { const response = await resms.send({ to: "+33123456789", text: "Your sign-in code is 123456", }); console.log('SMS sent successfully:', response); } catch (error) { console.error('Failed to send SMS:', error); }

Going Further


Need help? Contact our support at contact@resms.dev

Last updated on