Skip to main content

JSend response models and utils for FastAPI

Project description

fastapi-jsend

A lightweight FastAPI extension to return responses in JSend format.

🚀 Install

pip install fastapi-jsend

📦 Usage

fastapi-jsend helps you build clean, consistent API responses using the JSend specification. It simplifies success, fail, and error responses by providing utility functions and a base response class for FastAPI.


🔧 Basic Setup

Set JSendResponse as the default response class for your FastAPI app:

from fastapi import FastAPI
from fastapi_jsend import JSendResponse, jsend_success, jsend_fail, jsend_error

app = FastAPI(default_response_class=JSendResponse)

@app.get("/ping")
async def ping():
    return jsend_success(data={"message": "pong"})

✅ Success Response

Use jsend_success(data) to return a successful response with payload:

@app.get("/user/{user_id}")
async def get_user(user_id: int):
    user = {"id": user_id, "name": "John Doe"}
    return jsend_success(data=user)

Response:

{
  "status": "success",
  "data": {
    "id": 1,
    "name": "John Doe"
  }
}

⚠️ Fail Response

Use jsend_fail(data) for failed operations due to user input, validation, or missing data:

@app.get("/validate-age/{age}")
async def validate_age(age: int):
    if age < 18:
        return jsend_fail(data={"error": "User must be at least 18 years old"})
    return jsend_success(data={"age": age})

Response:

{
  "status": "fail",
  "data": {
    "error": "User must be at least 18 years old"
  }
}

❌ Error Response

Use jsend_error(message, code=None, data=None) for internal or unexpected errors:

@app.get("/error-demo")
async def error_demo():
    try:
        raise ValueError("Something went wrong")
    except Exception as e:
        return jsend_error(message=str(e))

Response:

{
  "status": "error",
  "message": "Something went wrong"
}

🔍 About JSend

JSend is a specification that standardizes how JSON responses are formatted:

  • success: The request was successful and the response contains data.
  • fail: The request was valid but failed due to a known issue (e.g., validation).
  • error: An unexpected issue occurred during request processing.

Using this format ensures your APIs are predictable, consistent, and easy to consume on the frontend.


💡 Tips

  • You can override the response format per endpoint using response_class=JSendResponse.
  • Works seamlessly with FastAPI dependencies, middlewares, and routers.
  • Combine with FastAPI response models for even stronger type guarantees.

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

fastapi_jsend-0.1.1.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.

fastapi_jsend-0.1.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_jsend-0.1.1.tar.gz.

File metadata

  • Download URL: fastapi_jsend-0.1.1.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for fastapi_jsend-0.1.1.tar.gz
Algorithm Hash digest
SHA256 99cccf6d05283834a35878f2cb404e02d63450fe70aa534fccb40fe0010e5104
MD5 7306ca1e6ab9606f157d217f61b29da4
BLAKE2b-256 1d16667a2c52a4abf7491f6391626d34a965d5124779cd29d18c3b59386c8d8e

See more details on using hashes here.

File details

Details for the file fastapi_jsend-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: fastapi_jsend-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for fastapi_jsend-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d1ff036c2141a1c6ae4f2df04d95bb10cdb457b379fc48ea6103a9d394533d6d
MD5 214cc9fd3032c510ef8d974cb47fd853
BLAKE2b-256 011fc24a35078889591af4e69d9ce119425e6988627cfdbea2efc03f5fd4b685

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