API docs

Node.js SDK

Call the WeSend API from Node.js with the official @wesend/node package.

The SDK gives every endpoint of this API as a typed method. It has no dependencies, works with TypeScript and JavaScript, and retries a failed request without ever sending a message twice.

npm install @wesend/node

Requires Node.js 20 or newer. Works with ESM (import) and CommonJS (require).

Quick start

Keep your API key in the WESEND_API_KEY environment variable. Called with no key, as new WeSend(), the client reads it from there itself.

Node.js
import { WeSend } from "@wesend/node"

const wesend = new WeSend(process.env.WESEND_API_KEY)

const message = await wesend.sms.send({
  to: "99112233",
  text: "Tanii zahialga batalgaajlaa. #1042",
})

console.log(message.id, message.status, message.balance)

Use the SDK only on your server. A key in browser or mobile app code can be read by anyone and used to send with your credits.

Methods

Each method calls one endpoint and returns its response. Fields are named exactly as on the endpoint pages (from_name, batch_id, created_at); only the SDK's own options are camelCase (idempotencyKey, maxRetries).

MethodEndpoint
wesend.sms.send(params)POST /v1/sms
wesend.sms.sendBulk(params)POST /v1/sms/bulk
wesend.email.send(params)POST /v1/email
wesend.email.sendBulk(params)POST /v1/email/bulk
wesend.verify.create(params)POST /v1/verify
wesend.verify.check(id, { code })POST /v1/verify/{id}/check
wesend.verify.get(id)GET /v1/verify/{id}
wesend.messages.get(id)GET /v1/messages/{id}
wesend.batches.get(id)GET /v1/batches/{id}
wesend.balance.get()GET /v1/balance
wesend.pricing.get()GET /v1/pricing

On every endpoint page, the Node.js tab of a request example shows the SDK call.

Errors

A failed request throws a WeSendError. Branch on its code, which is one of the codes on the Errors page. Use message only for logs.

Node.js
import { WeSend, WeSendError } from "@wesend/node"

try {
  await wesend.sms.send({ to: "99112233", text: "Sain baina uu" })
} catch (error) {
  if (!(error instanceof WeSendError)) throw error

  if (error.code === "insufficient_credits") {
    console.log(`Need ${error.required} credits, have ${error.balance}`)
  } else {
    console.error(error.status, error.code, error.message)
  }
}
FieldMeaning
codeThe machine-readable code, such as invalid_request or insufficient_credits.
statusThe HTTP status, or null when no response arrived.
messageA description in English.
retryAfterSeconds to wait before trying again, from the Retry-After header. Otherwise null.
balance, requiredOn insufficient_credits: the credits you have and the credits needed.
attempts_remainingOn invalid_code: how many tries are left.
invalidOn invalid_request from a bulk send: the recipients that were refused, up to 50.
idOn provider_failed: the id of the failed message.
  • verify.check resolves when the code is right. A wrong code throws invalid_code, with the tries left in attempts_remaining.
  • Three codes come from the SDK itself and not from the API: timeout and connection_error (no answer arrived, so status is null), and unexpected_response (the answer did not come from the WeSend API).

Retries and idempotency

The SDK tries a failed request again by itself, up to 2 more times, whenever that cannot send a message twice.

FailureWhat the SDK does
429 rate_limitedWaits the Retry-After seconds and sends again. A wait longer than 10 seconds is thrown instead.
Network failure, timeout or 5xxSends again, for reads and for the four send methods.
502 provider_failedSends again: the operator did not accept the message and the credits are back.
Any other 4xxThrown at once. Fix the request first.

The four send methods (sms.send, sms.sendBulk, email.send, email.sendBulk) always carry an Idempotency-Key: the SDK makes one for each call, so its own retries never send twice. Pass your own key to stay safe when your code runs twice too, such as a queue job that is retried.

Node.js
await wesend.sms.send(
  { to: "99112233", text: "Tanii zahialga batalgaajlaa. #1042" },
  { idempotencyKey: "order-1042-confirmation" },
)
  • With your own key, a 502 provider_failed is not retried: a repeat under that key would only report the same failure. Send again with a new key.
  • verify.create and verify.check take no Idempotency-Key, so they are not retried after a network failure or a 5xx.

Options

Node.js
const wesend = new WeSend(process.env.WESEND_API_KEY, {
  timeout: 30_000,
  maxRetries: 2,
})
OptionDefaultDescription
timeout30000Milliseconds one attempt may take.
maxRetries2How many times a failed request is tried again. 0 turns retries off.
baseUrlhttps://api.wesend.mn/v1Where requests go.
fetchglobal fetchA fetch to use in place of the global one.