Skip to main content

A clean, efficient Flask-based API builder with authentication, routing, and error handling.

Project description

easy-api-builder

A lightweight Flask wrapper for building JSON APIs without boilerplate. Register endpoints, authentication, and error handling in a single fluent call, then consume any API with the built-in easy_request helper.


Installation

pip install flask requests werkzeug

Place api_builder.py in your project directory and import from it directly.


Quick start

from api_builder import ApiBuilder, easy_request

api = ApiBuilder()

api.get("/status", {"ok": True})
api.run()

Visit http://localhost:5000/status — done.


ApiBuilder

Constructor

api = ApiBuilder(
    name=__name__,         # Flask app name
    template_folder="templates",  # folder for HTML error pages
    debug=False,           # enable Flask debug mode
)

Registering endpoints

All registration methods return self, so calls can be chained.

.get(path, payload, *, auth_keys, handler)

# Static payload
api.get("/hello", {"message": "Hello, world!"})

# Key-protected
api.get("/secret", {"data": 42}, auth_keys=["abc123", "xyz789"])

# Custom handler
def my_handler():
    from flask import jsonify, request
    return jsonify({"echo": request.args.get("q")})

api.get("/echo", handler=my_handler)

.post(path, payload, *, auth_keys, handler)

Same signature as .get(), registers a POST endpoint.

from flask import jsonify, request

def handle_submit():
    body = request.get_json()
    return jsonify({"received": body})

api.post("/submit", handler=handle_submit)

.route(path, methods, payload, *, auth_keys, handler)

Register any HTTP verb or combination of verbs.

api.route("/ping", ["GET", "POST"], {"pong": True})

Starting the server

# Blocking — use for production-style scripts
api.run(host="0.0.0.0", port=5000)

# Non-blocking daemon thread — use when running alongside other code
thread = api.run_async(port=5000)

Authentication

Pass auth_keys to any registration method. Requests must include ?key=<value> in the URL.

api.get("/private", {"data": "secret"}, auth_keys=["abc123"])
Scenario Status Response
No key supplied 401 {"error": "API key required."}
Wrong key 403 {"error": "Invalid API key."}
Valid key 200 Your payload

Error handling

All HTTP errors are returned as JSON automatically — no setup required.

{
  "code": 404,
  "name": "Not Found",
  "description": "..."
}

If a templates/404.html file exists it will be rendered for 404s; otherwise the JSON fallback is used.


easy_request

A thin wrapper around requests with built-in error handling.

easy_request(
    url,               # target URL (required)
    method="GET",      # HTTP verb
    params=None,       # URL query parameters (dict)
    json_body=None,    # request body sent as JSON (dict)
    headers=None,      # extra headers (dict)
    timeout=10,        # socket timeout in seconds
    api_key=None,      # appended as ?key= automatically
)

Returns a parsed JSON dict on success, or a safe error dict on failure.

# Simple GET
data = easy_request("https://api.example.com/users")

# Authenticated
data = easy_request("http://localhost:5000/secret", api_key="abc123")

# POST with body
data = easy_request(
    "http://localhost:5000/submit",
    method="POST",
    json_body={"name": "Alice"},
)

Error responses

{"error": "Request timed out.",  "url": "..."}          # timeout
{"error": "...",  "status_code": 403}                   # HTTP error
{"error": "Non-JSON response.",  "body": "..."}          # non-JSON body

Full example

from flask import jsonify, request
from api_builder import ApiBuilder, easy_request

api = ApiBuilder()

# Public status endpoint
api.get("/status", {"ok": True})

# Protected data endpoint
api.get("/data", {"value": 99}, auth_keys=["secret-key"])

# Dynamic POST handler
def echo():
    return jsonify(request.get_json())

api.post("/echo", handler=echo)

# Start in background thread
api.run_async(port=5000)

# Consume the protected endpoint
result = easy_request("http://localhost:5000/data", api_key="secret-key")
print(result)  # {"value": 99}

License

MIT

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

easy_api_builder-2026.1.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

easy_api_builder-2026.1.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file easy_api_builder-2026.1.0.tar.gz.

File metadata

  • Download URL: easy_api_builder-2026.1.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for easy_api_builder-2026.1.0.tar.gz
Algorithm Hash digest
SHA256 0d2a3bf654d876dfa1a6f6374b4336abe813254b92db96fd3b84c071ef31d15c
MD5 084f173ed3e67d595074f57581fcb3cf
BLAKE2b-256 abdf9b475241d9052c752a58597959446eb3bb9247b315aaa98f1ca02dba238f

See more details on using hashes here.

File details

Details for the file easy_api_builder-2026.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for easy_api_builder-2026.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8ad69b887a2c111afa43a75b0f0072d220c27e0dba75aacfa96b0471431f4e7b
MD5 51906f9b000f71b7c862d177909cb4a2
BLAKE2b-256 cc39ab1161f0f6931e90d428eaa174adc9d02e1c954126476147502df11b7188

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