Building a Light API Integration Proxy in Django (Why & How)

dev.to

Why another API tool?

As I built more complex applications, I noticed a pattern: every API has its own auth, rules, and formats. Setting up a full API Gateway like Kong felt like overkill for my needs.

I wanted a single tool to handle execution, authentication, and logging so my app only deals with business logic.


The Architecture (PoC Stage)

To validate the concept quickly, I built a synchronous MVP using:

  • Django & PostgreSQL: To easily model endpoints, params, and keys.

  • Requests / Urllib: To dynamic construct downstream calls.

⚠️ Note: I chose Django for rapid prototyping. I plan to migrate to Async (HTTPX / FastAPI or Go) once the execution logic is stable.


How it Works

  1. Register the API: Define endpoints, base URLs, and OAuth/API Keys in the DB.

  2. Execute via a single endpoint:

python

# Instead of handling OAuth + custom headers manually in your app:

import os
import requests
import json
from dotenv import load_dotenv

# Load variables from the .env file
load_dotenv()


# ==========================================
# CONFIGURATION
# ==========================================
# 1. Your Asstgr API Key (generated from your admin panel or the API)
API_KEY = os.getenv("ASSTGR_API_KEY")

# 2. The endpoint ID of '/user' linked to the GitHub API (ID: 36)
# (If you don't know it, run a GET request to http://localhost:8000/api/v1/apis/36/ to find it)
ENDPOINT_ID = "10"

# 3. The base URL of your local Asstgr instance
BASE_URL = "http://localhost:8000/api/v1"

# ==========================================
# REQUEST SETUP
# ==========================================
# Target URL for Asstgr's unified execution route
url = f"{BASE_URL}/apis/36/endpoints/{ENDPOINT_ID}/execute/"

# Required HTTP headers (Asstgr API Key Authentication)
headers = {
    "Authorization": f"Api-Key {API_KEY}",
    "Content-Type": "application/json"
}

# Request body payload
# Requesting 'json' format to retrieve the full raw GitHub user profile
payload = {
    "method": "GET",
    "params": {},
    "display_format": "standard"
}

# ==========================================
# EXECUTION
# ==========================================
try:
    print(f"Connecting to Asstgr: {url}...")
    response = requests.post(url, headers=headers, json=payload)

    # Check if the HTTP request to Asstgr was successful (status code 2xx)
    response.raise_for_status()

    data = response.json()

    print("\n--- Response Received Successfully ---")
    print(json.dumps(data, indent=2, ensure_ascii=False))

except requests.exceptions.HTTPError as http_err:
    print(f"\n[HTTP Error] Server returned status code: {response.status_code}")
    try:
        # Try to print the detailed error JSON payload from Django REST Framework
        print(json.dumps(response.json(), indent=2))
    except ValueError:
        print(response.text)
except Exception as err:
    print(f"\n[Error] An unexpected error occurred: {err}")
Enter fullscreen mode Exit fullscreen mode

This project is in its early stages. Here is what I'm working on next:

[ ] Payload Encryption

[ ] Async Execution (HTTPX)

[ ] Caching with Redis

If you've built similar integration layers or have feedback on the approach, I'd love to hear your thoughts in the comments!

Check out the code on GitHub

Source: dev.to

arrow_back Back to News