S
docs- Words
- 663
- Reading time
- 3 min
- Last crawled
- 2026-09-27
- Language
- EN
cat smashsend.com/docs/api/tutorial.md
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 fetchconst apiKey = 'YOUR_API_KEY'; const response = await fetch('https://api.smashsend.com/v1/api-keys/check', { 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 JavaScriptconst apiKey = 'YOUR_API_KEY';const contactData = { properties: { email: 'jane.doe@example.com', firstName: 'Jane', lastName: 'Doe', company: 'Acme Inc' }}; 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); // Expected response:{ "contact": { "id": "ctc_B7OSYsnTUVwBPOGrgYTjvSGS", "createdAt": "2025-06-04T05:26:05.593Z", "updatedAt": "2025-06-04T05:26:05.593Z", "properties": { "email": "jane.doe@example.com", "firstName": "Jane", "lastName": "Doe", "company": "Acme Inc" }, "workspaceId": "wrk_ENGTK86JEXXckyDVJthKkPer" }}``` ## 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 campaignconst apiKey = 'YOUR_API_KEY';const campaignId = 'CAMPAIGN_ID';const contactId = 'ctc_B7OSYsnTUVwBPOGrgYTjvSGS'; // 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 subscriptionconst apiKey = 'YOUR_API_KEY';const webhookData = { url: 'https://your-app.com/webhook-endpoint', events: ['CONTACT_CREATED', 'CONTACT_UPDATED', 'CONTACT_DELETED'], token: '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); // Expected response:{ "webhook": { "id": "wh_123456789", "url": "https://your-app.com/webhook-endpoint", "events": ["CONTACT_CREATED", "CONTACT_UPDATED", "CONTACT_DELETED"], "token": "wht_a1b2c3d4e5f6g7h8i9j0", "createdAt": "2023-04-16T15:45:00Z" }}``` ## 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.jsconst express = require('express');const crypto = require('crypto');const app = express(); // Middleware to parse JSON requestsapp.use(express.json()); // Your webhook secret from Step 4const WEBHOOK_SECRET = 'YOUR_WEBHOOK_SECRET'; // Verify webhook signaturefunction verifyWebhookSignature(req) { const signature = req.headers['x-smashsend-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 endpointapp.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.event); switch (event.event) { case 'CONTACT_CREATED': // Handle new contact console.log('New contact created:', event.data.contact.id); break; case 'CONTACT_UPDATED': // Handle contact update console.log('Contact updated:', event.data.contact.id); break; case 'CONTACT_DELETED': // Handle contact deletion console.log('Contact deleted:', event.data.contact.id); break; default: console.log('Unhandled event type:', event.event); } // 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](https://smashsend.com/docs/api/contacts) for more advanced contact management features - Learn about [Contact properties API](https://smashsend.com/docs/api/contact-properties) for creating and managing campaigns - Learn about [Webhooks API](https://smashsend.com/docs/api/webhooks) for creating and managing campaigns - Learn about [API Keys API](https://smashsend.com/docs/api/api-keys) for creating and managing campaigns - Learn about [Users API](https://smashsend.com/docs/api/users) for creating and managing campaigns - Check out our [Postman Collection](https://smashsend.com/docs/api/postman) for ready-to-use API requests - Implement proper error handling and retry logic in your integration
