Skip to content

Integration Patterns

This reference document outlines best practices and common patterns for integrating Voucher API into your applications.

Common Integration Scenarios

E-commerce Integration

graph TD
    A[Customer] -->|Adds items| B[Cart]
    B -->|Applies voucher| C[Voucher API]
    C -->|Validates| D[Order]
    D -->|Processes| E[Payment]
  1. Cart Integration
  2. Validate vouchers during cart updates
  3. Apply discounts in real-time
  4. Handle multiple vouchers

  5. Checkout Flow

  6. Validate final order
  7. Apply confirmed discount
  8. Record redemption

Mobile App Integration

graph LR
    A[App] -->|API Calls| B[Backend]
    B -->|Validates| C[Voucher API]
    C -->|Returns| B
    B -->|Updates| A
  1. API Integration
  2. Use SDKs for native platforms
  3. Handle offline scenarios
  4. Implement retry logic

  5. User Experience

  6. Show real-time validation
  7. Display discount preview
  8. Handle errors gracefully

Implementation Examples

import { VouchersAPI } from '@voucher/vouchers-sdk';

const vouchers = new VouchersAPI({
  apiKey: 'your-api-key'
});

// Create a webhook handler
const webhookHandler = vouchers.createWebhookHandler({
  secret: 'your-webhook-secret'
});

// Handle incoming webhook events
app.post('/webhooks/voucher', async (req, res) => {
  try {
    const event = await webhookHandler.verify(req.body, req.headers['x-voucher-signature']);

    switch (event.type) {
      case 'voucher.created':
        console.log('New voucher created:', event.data);
        break;
      case 'voucher.applied':
        console.log('Voucher applied:', event.data);
        break;
    }

    res.status(200).send('Webhook processed');
  } catch (error) {
    console.error('Webhook error:', error);
    res.status(400).send('Invalid webhook');
  }
});
const { VouchersAPI } = require('@voucher/vouchers-sdk');
const express = require('express');
const app = express();

const vouchers = new VouchersAPI({
  apiKey: 'your-api-key'
});

// Create a webhook handler
const webhookHandler = vouchers.createWebhookHandler({
  secret: 'your-webhook-secret'
});

// Handle incoming webhook events
app.post('/webhooks/voucher', async (req, res) => {
  try {
    const event = await webhookHandler.verify(req.body, req.headers['x-voucher-signature']);

    switch (event.type) {
      case 'voucher.created':
        console.log('New voucher created:', event.data);
        break;
      case 'voucher.applied':
        console.log('Voucher applied:', event.data);
        break;
    }

    res.status(200).send('Webhook processed');
  } catch (error) {
    console.error('Webhook error:', error);
    res.status(400).send('Invalid webhook');
  }
});

Best Practices

  1. Error Handling
  2. Implement proper error handling
  3. Use retry mechanisms
  4. Log validation failures

  5. Performance

  6. Cache validation results
  7. Use batch operations
  8. Implement rate limiting

  9. Security

  10. Validate server-side
  11. Use secure API keys
  12. Implement proper authentication

See Also