ZooTools is now SMASHSENDRead update

SMASHSEND API Tutorial

This step-by-step tutorial will guide you through integrating with the SMASHSEND API. Learn how to authenticate, create resources, and handle responses.

Prerequisites

Before starting, make sure you have:

  • A SMASHSEND account with API access

  • An API key (which you can get from your dashboard)

  • Basic understanding of REST APIs and HTTP requests

Step 1: Authentication

All requests to the SMASHSEND API require authentication. You'll need to include your API key in the Authorization header of all HTTP requests.

// Example of an authenticated request using JavaScript fetch
const apiKey = 'YOUR_API_KEY';

const response = await fetch('https://api.smashsend.com/v1/users', {
  headers: {
    'Authorization': `Bearer ${apiKey}`,
    'Content-Type': 'application/json'
  }
});

const data = await response.json();
console.log(data);

Step 2: Create a Contact

Let's create a new contact through the API.

// Creating a contact with JavaScript
const apiKey = 'YOUR_API_KEY';
const contactData = {
  email: 'jane.doe@example.com',
  firstName: 'Jane',
  lastName: 'Doe',
  customFields: {
    company: 'Acme Inc',
    role: 'Marketing Manager'
  }
};

const response = await fetch('https://api.smashsend.com/v1/contacts', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${apiKey}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(contactData)
});

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

Step 3: Add Contact to a Campaign

Now that we have created a contact, let's add them to an existing campaign.

// Adding a contact to a campaign
const apiKey = 'YOUR_API_KEY';
const campaignId = 'CAMPAIGN_ID';
const contactId = 'CONTACT_ID'; // The ID from the previous response

const response = await fetch(`https://api.smashsend.com/v1/campaigns/${campaignId}/contacts`, {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${apiKey}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    contactId: contactId
  })
});

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

Step 4: Set Up a Webhook

Setting up webhooks allows you to receive real-time notifications when events occur in your SMASHSEND account.

// Creating a webhook subscription
const apiKey = 'YOUR_API_KEY';
const webhookData = {
  url: 'https://your-app.com/webhook-endpoint',
  events: ['CONTACT_CREATED', 'CONTACT_UPDATED', 'CAMPAIGN_PUBLISHED'],
  secret: 'YOUR_WEBHOOK_SECRET' // Used to verify webhook payloads
};

const response = await fetch('https://api.smashsend.com/v1/webhooks', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${apiKey}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(webhookData)
});

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

Step 5: Handling Webhook Events

Here's an example of how to handle webhook events on your server (using Express.js):

// Setting up a webhook handler with Express.js
const express = require('express');
const crypto = require('crypto');
const app = express();

// Middleware to parse JSON requests
app.use(express.json());

// Your webhook secret from Step 4
const WEBHOOK_SECRET = 'YOUR_WEBHOOK_SECRET';

// Verify webhook signature
function verifyWebhookSignature(req) {
  const signature = req.headers['x-zootools-signature'];
  const payload = JSON.stringify(req.body);
  
  const hmac = crypto.createHmac('sha256', WEBHOOK_SECRET);
  hmac.update(payload);
  const calculatedSignature = hmac.digest('hex');
  
  return crypto.timingSafeEqual(
    Buffer.from(signature),
    Buffer.from(calculatedSignature)
  );
}

// Webhook endpoint
app.post('/webhook-endpoint', (req, res) => {
  try {
    // Verify the webhook signature
    if (!verifyWebhookSignature(req)) {
      return res.status(401).send('Invalid signature');
    }
    
    // Process the webhook event
    const event = req.body;
    
    console.log('Received webhook event:', event.type);
    
    switch (event.type) {
      case 'CONTACT_CREATED':
        // Handle new contact
        break;
      case 'CONTACT_UPDATED':
        // Handle contact update
        break;
      case 'CAMPAIGN_PUBLISHED':
        // Handle campaign publication
        break;
      default:
        console.log('Unhandled event type:', event.type);
    }
    
    // Respond with a 200 OK
    res.status(200).send('Webhook received');
  } catch (error) {
    console.error('Webhook error:', error);
    res.status(500).send('Webhook processing error');
  }
});

app.listen(3000, () => {
  console.log('Webhook server running on port 3000');
});

Next Steps

Now that you've learned the basics of integrating with the SMASHSEND API, here are some next steps:

  • Explore the Contacts API for more advanced contact management features

  • Learn about Campaigns API for creating and managing campaigns

  • Check out our Postman Collection for ready-to-use API requests

  • Implement proper error handling and retry logic in your integration