Skip to main content

A middleware to monetize Django APIs with Stripe and the 402 Error Code

Project description

Django API Gate 🏦

Monetize your Django API in minutes. django-api-gate is a drop-in middleware that turns your API into a metered, paid service. It connects to the API Gate Hub protocol to handle API keys, credit balances, and billing automatically.

🚀 Features

Instant Paywall: Protects your API routes with one line of code.

Metered Billing: Deducts credits per request automatically.

Zero-Config Banking: No need to set up Stripe or Ledger logic yourself—API Gate Hub handles the backend.

High Performance: Uses persistent connection pooling for minimal latency impact (~20ms).

📦 Installation

pip install django-api-gate

⚙️ Configuration

  1. Add into your settings.py:

MIDDLEWARE = [

'django.middleware.security.SecurityMiddleware',

'django.contrib.sessions.middleware.SessionMiddleware',

'django.middleware.common.CommonMiddleware',

# ...

'django_api_gate.middleware.ApiGateMiddleware', # <--- Add this

# ...

]

  1. Go to API Gate Hub and create an account.

  2. Copy your API Key from the dashboard.

  3. Add your API Key to settings.py:

API_GATE_API_KEY = "your-merchant-uuid-here"

OPTIONAL VARIABLES:

Change which URLS are protected (Default: '/api/'):

API_GATE_URL_PREFIX = "/v1/"

Change the cost per request (Default: 1 credit):

API_GATE_PRICE = 5

🛠️ Usage

Once installed, any request to your protected URL prefix (e.g., /api/data) will require an API Gate Key in the header:

curl -H "X-Api-Key: 'customer-uuid-here'" https://yoursite.com/api/data

If a user runs out of credits, they receive a standard 402 Payment Required response:

{ "error": "Insufficient Credits", "balance": 0, "top_up_url": "https://api-gate-production.onrender.com/" }

If a user is missing a key, they receive a standard 402 Payment Required response:

{ "error": "Payment Required", "message": "Access requires a prepaid API Key in header 'X-Api-Key'.", "portal_url": "https://api-gate-production.onrender.com/" }

🏗️ Architecture

This library operates as a Spoke in a Hub-and-Spoke financial model.

The Hub: The API Gate Hub (Ledger, Stripe Processing, User Accounts).

The Spoke: Your Django App (Product, Logic, Value).

When a request comes in, this middleware pings the Hub to verify funds and transfer credits from the Consumer to the Merchant.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_api_gate-0.0.2.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_api_gate-0.0.2-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file django_api_gate-0.0.2.tar.gz.

File metadata

  • Download URL: django_api_gate-0.0.2.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for django_api_gate-0.0.2.tar.gz
Algorithm Hash digest
SHA256 f429f47fba501cd9d49f70e9f353aa11e86fd00abb1f4c25e88a15c7434716f7
MD5 68009238552111c4912c5a702afbe3ba
BLAKE2b-256 402e2e44bbaa15d5dde5648168fbf94afc27a6c1d63d29e434ff863df87adc3b

See more details on using hashes here.

File details

Details for the file django_api_gate-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_api_gate-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e23d810b6286d45fef80a4a1f2c460444582c3ff79b273253f4d1aa0daa340e3
MD5 1125ee1f67f8c793ab117c3150276843
BLAKE2b-256 4a7d62f735defd8a4dbc0adcb8f092448d6e22efd171fed7998669439b47251d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page