Back to Blog
Tutorial22 March 2025

Integrating OnePush API with Node.js and Express

Step-by-step guide to integrating OnePush transactional email API into your Node.js and Express application. Includes code examples and best practices.

O

OnePush Team

22 March 2025

Integrating OnePush API with Node.js and Express

Integrating OnePush API with Node.js and Express

This guide walks you through integrating OnePush into a Node.js Express application for sending transactional emails.

Installation

First, install the OnePush SDK:

npm install @onepush/sdk

Or use the REST API directly with axios or fetch:

npm install axios

Basic Setup

Using the SDK

const OnePush = require('@onepush/sdk');

const onepush = new OnePush({
  apiKey: process.env.ONEPUSH_API_KEY,
});

Using REST API

const axios = require('axios');

const onepushClient = axios.create({
  baseURL: 'https://api.onepush.app/v1',
  headers: {
    'Authorization': `Bearer ${process.env.ONEPUSH_API_KEY}`,
    'Content-Type': 'application/json',
  },
});

Sending Emails

Simple Email

async function sendWelcomeEmail(userEmail, userName) {
  try {
    const response = await onepush.emails.send({
      to: userEmail,
      from: 'noreply@yourdomain.com',
      subject: `Welcome, ${userName}!`,
      html: `
        <h1>Welcome to Our Platform!</h1>
        <p>Hi ${userName},</p>
        <p>Thanks for signing up. We're excited to have you!</p>
      `,
      text: `Welcome to Our Platform! Hi ${userName}, Thanks for signing up.`,
    });
    
    console.log('Email sent:', response.id);
    return response;
  } catch (error) {
    console.error('Failed to send email:', error);
    throw error;
  }
}

Using Templates

async function sendOrderConfirmation(order) {
  const response = await onepush.emails.send({
    to: order.customerEmail,
    from: 'orders@yourdomain.com',
    templateId: 'order-confirmation',
    templateData: {
      orderNumber: order.id,
      items: order.items,
      total: order.total,
      shippingAddress: order.shippingAddress,
    },
  });
  
  return response;
}

Express Route Example

app.post('/api/send-password-reset', async (req, res) => {
  const { email, resetToken } = req.body;
  
  try {
    const resetUrl = `https://yourapp.com/reset-password?token=${resetToken}`;
    
    await onepush.emails.send({
      to: email,
      from: 'noreply@yourdomain.com',
      subject: 'Reset Your Password',
      html: `
        <h2>Password Reset Request</h2>
        <p>Click the link below to reset your password:</p>
        <a href="${resetUrl}">Reset Password</a>
        <p>This link expires in 1 hour.</p>
      `,
    });
    
    res.json({ success: true });
  } catch (error) {
    console.error('Error sending email:', error);
    res.status(500).json({ error: 'Failed to send email' });
  }
});

Error Handling

Always handle errors gracefully:

try {
  await onepush.emails.send(emailData);
} catch (error) {
  if (error.response) {
    // API error
    console.error('API Error:', error.response.data);
  } else if (error.request) {
    // Network error
    console.error('Network Error:', error.message);
  } else {
    // Other error
    console.error('Error:', error.message);
  }
}

Best Practices

  • Store API keys in environment variables
  • Use async/await for better error handling
  • Implement retry logic for transient failures
  • Log all email sends for debugging
  • Use templates for consistent branding
  • Set up webhooks for event tracking

With OnePush, integrating transactional emails into your Node.js application is straightforward and reliable.