Backend Implementation Guide

The Delivery Options JavaScript Component requires a backend endpoint to securely handle authentication with the Metapack Delivery Options API.

Getting Started

Your backend needs to provide an endpoint (e.g., /api/token) that:

  1. Calls the Metapack Delivery Options authentication API

  2. Returns the authentication token to your frontend

Example Backend Endpoint (Node.js):

app.post('/api/token', async (req, res) => {
    try {
        const response = await fetch('https://ddo.metapack.com/auth/token',
{
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                username: process.env.DELIVERY_OPTIONS_USERNAME,
                password: process.env.DELIVERY_OPTIONS_PASSWORD
            })
        });

        if (!response.ok) {
            throw new Error(`Authentication failed: ${response.status}`);
        }

        const tokenData = await response.json();
        res.json(tokenData);

    } catch (error) {
        console.error('Delivery Options token error:', error);
        res.status(500).json({ error: 'Failed to obtain token' });
    }
});

Example Backend Endpoint (Python/Flask):

@app.route('/api/token', methods=['POST'])
    def get_ddo_token():
        try:
            response = requests.post(
                'https://ddo.metapack.com/auth/token',
                json={
                    'username': os.environ.get('DELIVERY_OPTIONS_USERNAME'),
                    'password': os.environ.get('DELIVERY_OPTIONS_PASSWORD')
                }
            )
            response.raise_for_status()
            return jsonify(response.json())
        except Exception as e:
            return jsonify({'error': 'Failed to obtain token'}), 500

Configuration

  1. Store Credentials securely:

    # .env file
    DELIVERY_OPTIONS_USERNAME=your_username
    DELIVERY_OPTIONS_PASSWORD=your_password
  2. Configure the component to use your endpoint:

    window.ddoComponent.init({
        containerId: 'ddo-component-container',
        backendUrl: 'https://ddo.metapack.com',
    
        getToken: async function() {
            const response = await fetch('/api/token', {
                method: 'POST'
            });
    
            if (!response.ok) {
                throw new Error('Failed to get token');
            }
    
            return await response.json();
        },
    
        senderDetails: {
            warehouseCode: 'UK',
            wh_cc: 'GB'
        }
    });

Troubleshooting

CORS Errors:

// Add CORS headers to your backend
app.use(cors({
    origin: 'https://your-domain.com'
}));

Authentication Failure

  • Verify Delivery Options credentials are correct

  • Check environment variables are loaded

  • Confirm Delivery Options API endpoint URL

Testing Your Endpoint:

curl -X POST http://localhost:3000/api/token

Expected response:

{
    "idToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
    "expiresIn": 3600
}