Skip to main content

fastapi-armor 🛡️

Production-ready error handling, logging, and Sentry monitoring for FastAPI — in one line.

Python FastAPI License: MIT PyPI version Tests


Why fastapi-armor?

Every production FastAPI app needs the same boilerplate: structured logging, clean error responses, and error monitoring. fastapi-armor wires all of that up automatically — no copy-pasting, no missing edge cases.

Before:

from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import JSONResponse
import logging, logging.handlers, sentry_sdk
# ... 60+ lines of setup code

app = FastAPI()

After:

from fastapi import FastAPI
from fastapi_armor import Armor

app = FastAPI()
Armor(app)  # ✅ Done.

Installation

pip install fastapi-armor-lib

The package is published as fastapi-armor-lib on PyPI and imported as fastapi_armor in code.


Quick Start

from fastapi import FastAPI
from fastapi_armor import Armor

app = FastAPI()

Armor(
    app,
    log_file="logs/app.log",
    sentry_dsn="https://your-dsn@sentry.io/123",  # optional
    environment="production",
)

@app.get("/")
def root():
    return {"status": "running"}

That's it. Your app now has:

  • ✅ Rotating file logs + console output
  • ✅ Clean JSON error responses for all exception types
  • ✅ Sentry integration (if DSN is provided)

What It Does

🔴 Error Handling

Exception Type Response
HTTPException {"detail": "..."} with original status code
RequestValidationError {"error": "Validation failed", "detail": [...]} with 422
Any other Exception {"error": "Internal server error"} with 500 (full traceback logged internally)

All responses use Content-Type: application/json; charset=utf-8.

📋 Logging

  • Rotating file logs — auto-creates the logs/ directory
  • Console output — for local development
  • Format: 2024-01-01 12:00:00 | ERROR | Traceback (most recent call last): ...
  • Named logger: fastapi_armor

🔍 Sentry (optional)

Initializes sentry_sdk with FastApiIntegration and StarletteIntegration when a DSN is provided. Private data is not sent by default.


Parameters

Parameter Type Default Description
app FastAPI required Your FastAPI application instance
log_file str "logs/app.log" Path to the rotating log file
log_max_bytes int 5_000_000 Max size per log file (5 MB)
log_backup_count int 3 Number of backup log files to keep
sentry_dsn str "" Sentry DSN — leave empty to disable
environment str "development" Environment tag sent to Sentry

Requirements

  • Python 3.9+
  • FastAPI 0.100+
  • sentry-sdk 1.40+ (installed automatically)

Development

git clone https://github.com/raghadalb-tech/fastapi-armor.git
cd fastapi-armor
pip install -e ".[dev]"
pytest tests/ -v

Test output:

tests/test_armor.py::test_armor_initializes_without_errors PASSED
tests/test_armor.py::test_http_exception_returns_clean_json PASSED
tests/test_armor.py::test_unhandled_exception_returns_internal_server_error PASSED
tests/test_armor.py::test_validation_error_returns_validation_failed PASSED

4 passed

License

MIT


Author

Raghad Al-Bishri · Full-Stack Engineer GitHub · LinkedIn · PyPI

Metadata

Release files for fastapi-armor-lib 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-armor-lib 0.1.1
File Size Uploaded Provenance Actions
fastapi_armor_lib-0.1.1.tar.gz 5.5 kB Without provenance Details for fastapi_armor_lib-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-armor-lib 0.1.1
File Interpreter ABI Platform Actions
fastapi_armor_lib-0.1.1-py3-none-any.whl Python 3 none any Details for fastapi_armor_lib-0.1.1-py3-none-any.whl

Total release size: 11.7 kB

Release files / fastapi_armor_lib-0.1.1.tar.gz

Download URL fastapi_armor_lib-0.1.1.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
48c95eabb4e068478a59425cee2628c44565f2d095e3b80823a35acd98c35e4b
BLAKE2b-256 checksum
How to use checksums
a828c89f95f811df08408e7eab03708e75e059646193bac72719b81b4a3142be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Release files / fastapi_armor_lib-0.1.1-py3-none-any.whl

Download URL fastapi_armor_lib-0.1.1-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6e13212f0f8fc16229a2a57daeef4a743c29dbef239227b25d6c80f466f8aa3f
BLAKE2b-256 checksum
How to use checksums
0dd5eff01dac6a53a813e4645b3c8da1519fdb93d10b94830460a742b7a227d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Security for fastapi-armor-lib 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page