# Send Webhooks with Django

This guide will show you how to send webhooks with Django using Hookdeck's [Outpost](https://hookdeck.com/outpost) — an open-source event dispatcher. Outpost gives you a world-class webhook and event destination infrastructure in minutes for 1/10th the cost of alternatives.

Outpost supports [Event Destinations](https://eventdestinations.org/), allowing you to deliver events to webhook endpoints, as well as to queues, and popular brokers or event gateways. These include AWS SQS, RabbitMQ, GCP Pub/Sub, Amazon EventBridge, Kafka, and more—all from a single platform.

The team behind it has drawn on their experience running [Hookdeck Event Gateway](https://hookdeck.com/) and delivering over 100 billion events to thousands of customers.

## Why use Outpost?

Sending webhooks can be trickier than you think. From managing fan‑out load, retries and backoff, to endpoint health management, and security/operational overhead. Outpost takes care of all of that so you only need to think about is how to send a webhook.

## How to Send Webhooks with Django + Outpost

Start by deploying an [Outpost instance](https://hookdeck.com/docs/outpost/guides/deployment) locally, via Docker, or deploy to production. For guidance, check out the [Outpost quickstart](https://hookdeck.com/docs/outpost/quickstarts) or the [GitHub repository](https://github.com/hookdeck/outpost).

## Django Support in Outpost

You can interact with Outpost using the [Python SDK](https://github.com/hookdeck/outpost/tree/main/sdks/outpost-python) or the REST API. Each SDK provides a convenient way to interact with the Outpost API, including publishing events, managing topics, and configuring destinations.

```bash
pip install outpost_sdk

```

## Set Your Admin API Key

Before you can start making calls to the Outpost API you need to set an API key. The API uses bearer token authentication with a token of your choice configured through the `API_KEY` environment variable.

Create a `.env` file in your Django project root:

```bash
ADMIN_API_KEY=<YOUR_BEARER_TOKEN_HERE>
OUTPOST_URL=http://localhost:3333

```

## Send Webhooks (Publish Events)

In order to send a webhook, you have to instantiate Outpost, create a tenant, and create a destination for that tenant. Then you're ready to publish events to it.

### 1. Create an Outpost instance

```python
with Outpost() as outpost

```

You can set the security parameters through the security optional parameter when initializing the SDK client instance. The selected scheme will be used by default to authenticate with the API for all operations that support it. For example:

```python
with Outpost(
    security=models.Security(
        admin_api_key="<YOUR_BEARER_TOKEN_HERE>",
    ),
) as outpost

```

### 2. Create a new tenant.

A tenant in Outpost represents a user/team/organization in your product. Use the `upsert()` method to idempotently create or update a tenant: `outpost.tenants.upsert()`

### 3. Create a destination for that tenant.

The `create()` method is used to specify a new destination for the tenant. A destination is a specific instance of a destination type. For example, a webhook destination with a particular URL.

The request body structure depends on the [destination type](https://hookdeck.com/docs/outpost/destinations/webhook). Here we're creating a destination for a webhook, but it could be SQS, RabbitMQ, etc.

```python
  res = outpost.destinations.create(destination_create={
        "tenant_id": tenant_id,
        "id": user-provided-id,
        "type": models.DestinationWebhook,
        "topics": models.TopicsEnum.WILDCARD_,
        "config": {
            "url": "https://example.com/webhook-receiver",
        },
    })

```

Note that we specify topics. A topic is a way to categorize events and is a common concept found in Pub/Sub messaging.

### 4. Publish an event for the created tenant.

You can now use the `publish()` method to send an event to the specified topic, routed to a specific destination.

```python
  res = outpost.publish(data={
        "user_id": "userid",
        "status": "active",
    }, id="evt_custom_123", tenant_id="<TENANT_ID>", destination_id="<DESTINATION_ID>", topic="topic.name", metadata={
        "source": "crm",
    })

```

`id`: Is optional but recommended. If left empty, it will be generated by hashing the topic, data, and timestamp.

`tenant_id`: The tenant ID to publish the event must match an existing tenant, otherwise it will be ignored.

`destination_id`: Optional. Used to force delivery to a specific destination regardless of the topic.

`topic`: Optional. Assumed to match ANY topic if left empty. If set, it must match one of the configured topics.

`metadata`: Arbitrary key-value mapping for event metadata.

`data`: The `data` JSON object will be sent as the webhook body. Now, every time you publish an event to this tenant, Outpost will send an HTTP POST request with the data payload to the destination URL.

Other fields:

`eligible_for_retry`: Optional, defaults to `true`. Controls whether an event should be automatically retried.

`retries`: Configuration to override the default retry behavior of the client.

## Complete Example

Combining the examples above, you should end up with a Django application that looks like the following:

First, create an Outpost service file (`services/outpost_client.py`):

```python
import os
from dotenv import load_dotenv
from outpost_sdk import Outpost, models

load_dotenv()

class OutpostService:
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super(OutpostService, cls).__new__(cls)
            cls._instance._initialize()
        return cls._instance

    def _initialize(self):
        admin_api_key = os.getenv("ADMIN_API_KEY")
        server_url = os.getenv("OUTPOST_URL", "http://localhost:3333")

        if not admin_api_key:
            raise ValueError("Please set the ADMIN_API_KEY environment variable.")

        self.client = Outpost(
            security=models.Security(admin_api_key=admin_api_key),
            server_url=f"{server_url}/api/v1",
        )

    def get_client(self):
        return self.client

```

Then, create Django views (`webhooks/views.py`):

```python
import os
import json
import uuid
from django.http import JsonResponse
from django.views.decorators.http import require_http_methods
from django.views.decorators.csrf import csrf_exempt
from outpost_sdk import models
from services.outpost_client import OutpostService

outpost_service = OutpostService()
outpost = outpost_service.get_client()

@require_http_methods(["POST"])
def webhook_setup(request):
    """
    Create a new tenant and destination for webhook delivery.
    """
    try:
        tenant_id = f"tenant_{str(uuid.uuid4())}"
        topic = "user.created"
        destination_name = f"My Test Destination {str(uuid.uuid4())}"

        # 1. Create a tenant
        print(f"Creating tenant: {tenant_id}")
        tenant = outpost.tenants.upsert(tenant_id=tenant_id)
        print("Tenant created successfully:", tenant)

        # 2. Create a destination for the tenant
        print(f"Creating destination: {destination_name} for tenant {tenant_id}...")
        destination = outpost.destinations.create(
            tenant_id=tenant_id,
            destination_create=models.DestinationCreateWebhook(
                type=models.DestinationCreateWebhookType.WEBHOOK,
                config=models.WebhookConfig(
                    url="https://example.com/webhook-receiver"
                ),
                topics=[topic],
            ),
        )
        print("Destination created successfully:", destination)

        return JsonResponse({
            "success": True,
            "message": "Tenant and destination created successfully",
            "data": {
                "tenant_id": tenant.id if hasattr(tenant, 'id') else tenant_id,
                "destination_id": destination.id if hasattr(destination, 'id') else None,
            },
        })

    except Exception as e:
        print(f"An error occurred: {e}")
        return JsonResponse({
            "success": False,
            "error": str(e),
        }, status=500)

@require_http_methods(["POST"])
def publish_event(request):
    """
    Publish an event to a tenant's topic.
    """
    try:
        body = json.loads(request.body)
        tenant_id = body.get("tenant_id")
        topic = body.get("topic")

        if not tenant_id or not topic:
            return JsonResponse({
                "success": False,
                "error": "tenant_id and topic are required",
            }, status=400)

        event_payload = {
            "user_id": "user_456",
            "order_id": "order_xyz",
            "timestamp": str(uuid.uuid4()),
        }

        print(f"Publishing event to topic {topic} for tenant {tenant_id}...")
        event = outpost.publish(
            data=event_payload,
            tenant_id=tenant_id,
            topic=topic,
            eligible_for_retry=True,
        )

        print("Event published successfully")
        return JsonResponse({
            "success": True,
            "message": "Event published successfully",
            "event_id": event.id if hasattr(event, 'id') else None,
        })

    except json.JSONDecodeError:
        return JsonResponse({
            "success": False,
            "error": "Invalid JSON",
        }, status=400)
    except Exception as e:
        print(f"An error occurred: {e}")
        return JsonResponse({
            "success": False,
            "error": str(e),
        }, status=500)

@require_http_methods(["POST"])
@csrf_exempt
def webhook_receiver(request):
    """
    Receive and process incoming webhooks from Outpost.
    """
    try:
        body = json.loads(request.body)
        print("Webhook received:", body)

        return JsonResponse({
            "success": True,
            "message": "Webhook received",
        })

    except json.JSONDecodeError:
        return JsonResponse({
            "success": False,
            "error": "Invalid JSON",
        }, status=400)
    except Exception as e:
        print(f"An error occurred: {e}")
        return JsonResponse({
            "success": False,
            "error": str(e),
        }, status=500)

@require_http_methods(["GET"])
def get_portal_url(request, tenant_id):
    """
    Get the Tenant User Portal URL for a specific tenant.
    """
    try:
        portal_url = outpost.tenants.get_portal_url(tenant_id=tenant_id)

        return JsonResponse({
            "success": True,
            "portal_url": portal_url.url if hasattr(portal_url, 'url') else None,
        })

    except Exception as e:
        print(f"An error occurred: {e}")
        return JsonResponse({
            "success": False,
            "error": str(e),
        }, status=500)

```

Finally, configure your URL routing (`webhooks/urls.py`):

```python
from django.urls import path
from . import views

urlpatterns = [
    path("setup/", views.webhook_setup, name="webhook_setup"),
    path("publish/", views.publish_event, name="publish_event"),
    path("receiver/", views.webhook_receiver, name="webhook_receiver"),
    path("portal/<str:tenant_id>/", views.get_portal_url, name="get_portal_url"),
]

```

Add the webhook URLs to your main project's `urls.py`:

```python
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path("admin/", admin.site.urls),
    path("webhooks/", include("webhooks.urls")),
]

```

### Expose Tenant User Portal

Your application can offer the user the option to configure their destination, view and retry their events using a themeable [Tenant User Portal](https://hookdeck.com/docs/outpost/features/tenant-user-portal). Note that if the portal is used, then the Outpost API needs to be exposed to the public internet (or proxy requests to the Outpost API).

You can use `get_portal_url()` to return a redirect URL containing a JWT to authenticate the user with the portal. Then redirect your users to this URL or use it in an iframe in your Django templates.

#### Build your own UI

While Outpost offers a user portal, you may want to build your own UI for users to manage their destinations and view their events. The portal is built using the Outpost API with JWT authentication. You can leverage the same API to [build your own UI](https://hookdeck.com/docs/outpost/guides/building-your-own-ui).

## Help with debugging and testing webhooks

To help you and your users work with webhooks, we provide some additional tools. Like [Hookdeck Console](https://console.hookdeck.com/), a web-based inspector to test and debug incoming webhooks. As well as the [Hookdeck CLI](https://hookdeck.com/docs/cli) to forward webhook events to a local web server.

## More information

That's everything you need to start sending webhooks with Outpost using Django. For more information, please check out the [Outpost documentation](https://hookdeck.com/docs/outpost/overview).

If you have any questions, we'd love to help. Please join the [Hookdeck community on Slack](https://join.slack.com/t/hookdeckdevelopers/shared_invite/zt-yw7hlyzp-EQuO3QvdiBlH9Tz2KZg5MQ).

### Related resources

* [Outpost Documentation](https://hookdeck.com/docs/outpost/overview)
* [GitHub Repository](https://github.com/hookdeck/outpost)
* [Python SDK](https://github.com/hookdeck/outpost/tree/main/sdks/outpost-python)
* [Event Destinations Initiative](https://eventdestinations.org/)
* [Django Documentation](https://docs.djangoproject.com/)
* [Django Views](https://docs.djangoproject.com/en/stable/topics/http/views/)