Webhook and API Key Setup

Efficiently set up webhooks and API keys with concise instructions.

1. Create Webhook and API Key

Use the combined endpoint to create both a webhook and its API key in one request:

async function createWebhookAndApiKey() {
  const response = await fetch('https://backend.sa.amwal.tech/api/create-webhook-and-apikey/', {
    method: 'POST',
    headers: {
      'Authorization': 'YOUR_MERCHANT_SECRET_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      url: 'https://your-domain.com/webhooks/amwal',
      description: 'Production webhook for order events',
      event_type_names: ['order.created', 'order.success', 'order.failed'],
      api_key_name: 'Production Webhook Key',
      api_key_scopes: ['trigger_events', 'manage_webhooks']
    })
  });

  const data = await response.json();
  console.log('Webhook created:', data);
  console.warn('⚠️ Store this private key securely - it will not be shown again!');
  console.log('Private Key:', data.api_key.private_key);
  return data;
}

createWebhookAndApiKey();

Response:

{
  "webhook": {
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "url": "https://your-domain.com/webhooks/amwal",
    "description": "Production webhook for order events"
  },
  "api_key": {
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "name": "Production Webhook Key",
    "fingerprint": "8a7d42f1c4e6ba957beec92f2cad51d0b3ec4f8c9a1529e8f35e53a1de1a8b3b",
    "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0B...",
    "message": "Store this private key securely. It won't be shown again."
  }
}
⚠️

Important

Store the private key securely immediately.

2. Separate Webhook and API Key Creation

Create Webhook Endpoint:

async function createWebhookEndpoint() {
  const response = await fetch('https://backend.sa.amwal.tech/api/webhook-endpoints/', {
    method: 'POST',
    headers: {
      'X-API-Key': 'YOUR_API_KEY_FINGERPRINT',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      url: 'https://your-domain.com/webhooks/amwal',
      description: 'Order event notifications',
      event_types: [1, 2, 3],
      is_active: true
    })
  });

  return await response.json();
}

Create API Key:

async function createApiKey(webhookEndpointId) {
  const response = await fetch('https://backend.sa.amwal.tech/api/api-keys/', {
    method: 'POST',
    headers: {
      'Authorization': 'YOUR_MERCHANT_SECRET_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      name: 'Webhook API Key',
      webhook_endpoint: webhookEndpointId,
      is_active: true,
      scopes: ['trigger_events', 'manage_webhooks']
    })
  });

  return await response.json();
}