Skip to content
Code Examples

API Code Samples

Ready-to-use code examples to help you integrate our API quickly. Choose your preferred programming language and start sending mail programmatically.

Send a Simple Letter

Send a basic letter with JavaScript using the fetch API

send-letter.js
// Browser JavaScript implementation
const sendLetter = async () => {
  try {
    const response = await fetch('https://api.intelliprint.net/v1/prints', {
      method: 'POST',
      headers: {
        'Authorization': 'Bearer YOUR_API_KEY',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        document: 'base64_encoded_file',
        recipients: [{
          name: 'John Smith',
          address: {
            line1: '123 Main St',
            city: 'London',
            postcode: 'SW1A 1AA'
          }
        }],
        options: {
          color: 'full',
          duplex: true
        }
      })
    });

    const data = await response.json();
    console.log('Letter sent successfully:', data);
    return data;
  } catch (error) {
    console.error('Error sending letter:', error);
    throw error;
  }
};

Send Multiple Letters with Batch Processing

Send multiple letters in a single request using batch processing

batch-letters.js
// Node.js batch processing implementation
const axios = require('axios');

const sendBatchLetters = async (recipientData) => {
  try {
    const response = await axios.post(
      'https://api.intelliprint.net/v1/prints/batch',
      {
        template: 'base64_encoded_template',
        merge_fields: recipientData.map(recipient => ({
          recipient_name: recipient.name,
          recipient_address: recipient.address,
          custom_field_1: recipient.customField1,
          custom_field_2: recipient.customField2
        })),
        options: {
          color: 'full',
          duplex: true,
          envelope_type: 'c5'
        },
        testmode: false
      },
      {
        headers: {
          'Authorization': 'Bearer YOUR_API_KEY',
          'Content-Type': 'application/json'
        }
      }
    );
    
    console.log('Batch letters sent successfully:', response.data);
    return response.data;
  } catch (error) {
    console.error('Error sending batch letters:', error.response?.data || error.message);
    throw error;
  }
};

// Example usage
const recipients = [
  {
    name: 'John Smith',
    address: {
      line1: '123 Main St',
      city: 'London',
      postcode: 'SW1A 1AA'
    },
    customField1: 'Value 1',
    customField2: 'Value 2'
  },
  {
    name: 'Jane Doe',
    address: {
      line1: '456 High St',
      city: 'Manchester',
      postcode: 'M1 1AA'
    },
    customField1: 'Value 3',
    customField2: 'Value 4'
  }
];

sendBatchLetters(recipients);

Webhook Implementation

Implement webhooks to receive status updates about your mail

webhook-handler.js
// Express.js webhook handler
const express = require('express');
const crypto = require('crypto');
const app = express();

// Parse JSON bodies
app.use(express.json());

// Your webhook secret from Intelliprint dashboard
const WEBHOOK_SECRET = 'your_webhook_secret';

app.post('/webhooks/intelliprint', (req, res) => {
  const signature = req.headers['x-intelliprint-signature'];
  
  // Verify webhook signature for security
  const hmac = crypto.createHmac('sha256', WEBHOOK_SECRET);
  const calculatedSignature = hmac.update(JSON.stringify(req.body)).digest('hex');
  
  if (signature !== calculatedSignature) {
    console.error('Invalid webhook signature');
    return res.status(401).send('Invalid signature');
  }
  
  // Process the webhook event
  const event = req.body;
  
  switch (event.type) {
    case 'print.created':
      console.log('Print job created:', event.data.id);
      break;
    case 'print.processed':
      console.log('Print job processed:', event.data.id);
      break;
    case 'print.shipped':
      console.log('Print job shipped:', event.data.id);
      // Update your database or notify your users
      break;
    case 'print.delivered':
      console.log('Print job delivered:', event.data.id);
      // Update your database or notify your users
      break;
    case 'print.failed':
      console.error('Print job failed:', event.data.id, event.data.error);
      // Handle the failure appropriately
      break;
    default:
      console.log('Unknown event type:', event.type);
  }
  
  // Acknowledge receipt of the webhook
  res.status(200).send('Webhook received');
});

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

Additional Resources

Explore these resources to get the most out of our API

GitHub Repository

Check out our official GitHub repository for SDKs, example projects, and integration libraries.

API Reference

Explore our comprehensive API documentation for detailed endpoint references and guides.

SDK Downloads

Download our official SDKs to integrate Intelliprint into your applications more easily.

Ready to Start Coding?

Sign up for an account, get your API keys, and start sending mail programmatically today.