Docs/Send a Notification

JavaScript

Last updated·1 min read

Send push notifications from any JavaScript environment — browser, Node.js, or serverless functions.

Basic example

Works in the browser and Node.js 18+ (native fetch):

const response = await fetch('https://thenotification.app/api/sendNotification', {
  method: 'POST',
  headers: {
    'app_key': 'YOUR_API_KEY',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    title: 'New Message',
    body: 'Check out this notification',
    link: 'https://example.com/page',
    image: 'https://picsum.photos/400/200'
  })
})

const result = await response.json()
console.log(result)

Reusable function

async function sendNotification({ title, body, link, image } = {}) {
  const response = await fetch('https://thenotification.app/api/sendNotification', {
    method: 'POST',
    headers: {
      'app_key': process.env.NOTIFICATION_API_KEY,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({ title, body, link, image })
  })

  if (!response.ok) {
    const error = await response.json()
    throw new Error(error.error || 'Failed to send notification')
  }

  return response.json()
}

// Usage
await sendNotification({
  title: 'Deployment complete',
  body: 'Your app is live'
})

Node.js older versions

For Node.js below 18, install node-fetch:

npm install node-fetch
import fetch from 'node-fetch'

Next.js / server actions

Always call the API from the server side — never expose your API key in client-side code:

// app/actions/notify.js
'use server'

export async function sendNotification(title, body) {
  const response = await fetch('https://thenotification.app/api/sendNotification', {
    method: 'POST',
    headers: {
      'app_key': process.env.NOTIFICATION_API_KEY,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({ title, body })
  })

  return response.json()
}

Response handling

// Success
{
  "success": true,
  "message": "Notification sent to 1 device(s)",
  "devices_sent": 1,
  "failed_count": 0
}

// Error
{
  "success": false,
  "error": "Invalid app_key"
}