Skip to main content

NatsAPI

NatsAPI is a framework to develop Python3 applications that uses nats as communication medium instead of http. With nats you have a smaller footprint, faster req/s, pub/sub and better observability. NatsAPI is highly inspired by FastAPI and has the same development style. NatsAPI produces an AsyncAPI schema out of the box, this schema is not fully compatible with the standard waiting for version 3.0.0 to support request/reply pattern.

Table of Contents

Python and pydantic support

This library has support for python > 3.10 and pydantic v1 and v2.

Quickstart

Optional: Run a nats server in docker so you are able to connect: docker run --rm --name nats -dp 4222:4222 nats:latest -js -DV

$ git clone git@github.com:wegroupwolves/natsapi.git && cd natsapi
$ poetry install
$ poetry run python examples/minimal.py
$ nats request natsapi.persons.greet '{"params": {"person": {"first_name": "Foo", "last_name": "Bar"}}}'                                                                                                    

the output should look as follows:

18:19:00 Sending request on "natsapi.persons.greet"
18:19:00 Received on "_INBOX.dpBgTyG9XC5NhagdqRHTcp.eMkVkru8" rtt 1.052463ms
{"jsonrpc": "2.0", "id": "c2bc2d20-dbd5-4e39-a22d-c22a8631c5a3", "result": {"message": "Greetings Foo Bar!"}, "error": null}

Usage

Docs

UNDER CONSTRUCTION

Examples

Basic

from natsapi import NatsAPI, SubjectRouter
from pydantic import BaseModel


class Message(BaseModel):
    message: str


class Person(BaseModel):
    first_name: str
    last_name: str


app = NatsAPI("natsapi")
router = SubjectRouter()


@router.request("persons.greet", result=Message)
async def greet_person(app: NatsAPI, person: Person):
    return {"message": f"Greetings {person.first_name} {person.last_name}!"}


app.include_router(router)

if __name__ == "__main__":
    app.run()

Run as follows:

$ python app.py

Docs will be rendered as:

Example of redoc

Send a request:

from natsapi import NatsAPI
import asyncio


async def main():
    app = await NatsAPI("client").startup()

    params = {"person": {"first_name": "Foo", "last_name": "Bar"}}
    r = await app.nc.request("natsapi.persons.greet", params=params, timeout=5)
    print(r.result)

asyncio.run(main())

#> {'message': 'Greetings Foo Bar!'}

or on the command line

$ nats request natsapi.persons.greet '{"params": {"person": {"first_name": "Foo", "last_name": "Bar"}}}'                                                                                                    

18:19:00 Sending request on "natsapi.persons.greet"
18:19:00 Received on "_INBOX.dpBgTyG9XC5NhagdqRHTcp.eMkVkru8" rtt 1.052463ms
{"jsonrpc": "2.0", "id": "c2bc2d20-dbd5-4e39-a22d-c22a8631c5a3", "result": {"message": "Greetings Foo Bar!"}, "error": null}

Error handling with sentry

from natsapi import NatsAPI, SubjectRouter
import logging
from pydantic import ValidationError
from sentry_sdk import configure_scope
from natsapi.models import JsonRPCRequest, JsonRPCError
from pydantic import BaseModel


class StatusResult(BaseModel):
    status: str


app = NatsAPI("natsapi-example")

router = SubjectRouter()


@router.request("healthz", result=StatusResult)
async def handle_user(app: NatsAPI):
    return {"status": "OK"}


app.include_router(router)


def configure_sentry(auth):
    with configure_scope() as scope:
        scope.user = {
            "email": auth.get("email"),
            "id": auth.get("uid"),
            "ip_address": auth.get("ip_address"),
        }


@app.exception_handler(ValidationError)
async def handle_validation_exception(exc: ValidationError, request: JsonRPCRequest, subject: str) -> JsonRPCError:
    auth = request.params.get("auth") or {}
    configure_sentry(auth)
    logging.error(
        exc,
        exc_info=True,
        stack_info=True,
        extra={"auth": auth, "json": request.dict(), "subject": subject, "NATS": True, "code": -32003},
    )

    return JsonRPCError(code=-90001, message="VALIDATION_ERROR", data={"error_str": str(exc)})


if __name__ == "__main__":
    app.run(reload=False)

Generating documentation (asyncapi)

To see the documentation, you can use the binary to run the server. Root path is natsapi-example so:

$ ./nats-redoc 4222 master.trinity-testing

Server running
Docs can be found on localhost:8090
connected to nats on port 4222

When surfing to localhost:8090, the documentation should look like this:

Example of redoc

Plugins

Plugins can be added and are found in natsapi/plugin.py.

  • natsapi_mock: A handy mock fixture to intercept nats requests and to fake nats responses for any subject.

Roadmap

  • Add Request/Reply AsyncApi support
  • Hot reloading (when saving source code, application should be reloaded)
  • Fancy readme
  • Better benchmark
  • Add support for 'side effect' testing so that you can have more than 1 response for same mocked route, based on ordering. See respx
  • Better CI/CD -> with Python 3.13
  • Make nkeys optional
  • Pydantic V2 support (nice to have)

Metadata

Release files for natsapi 0.1.4

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

Source distribution (sdist)

Source distribution for natsapi 0.1.4
File Size Uploaded
natsapi-0.1.4.tar.gz 24.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for natsapi 0.1.4
File Interpreter ABI Platform
natsapi-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 53.5 kB

Release files / natsapi-0.1.4.tar.gz

Download URL natsapi-0.1.4.tar.gz
Size 24.3 kB
Tags Source
SHA-256 checksum
How to use checksums
e5ac1617f82fcf7ca68b7cfbc06be8f58217aced0733173781feb0739d14cf04
BLAKE2b-256 checksum
How to use checksums
4a52fd1c4b96bacac66d3d017c4d8d33fae96eccee8b8ed7dbbec3362fe62be5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.14.4 Linux/7.0.5-arch1-1

Release files / natsapi-0.1.4-py3-none-any.whl

Download URL natsapi-0.1.4-py3-none-any.whl
Size 29.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6a3aadcaad0e1e5881ead5ae984ae05b1221064d085d1f45473ca5015c6c258b
BLAKE2b-256 checksum
How to use checksums
7948fffe034546b4802fa58579c2389e72be98658c6ce6e95e8f6ab98e7edbaa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.14.4 Linux/7.0.5-arch1-1
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