Skip to main content

Starlette API

API power up for Starlette

Build Status Coverage Package version


Documentation: https://starlette-api.perdy.io


Starlette API

Starlette API aims to bring a layer on top of Starlette to provide a fast and easy way for building highly performant REST APIs.

It is production-ready and provides the following:

  • Generic classes for API resources that provides standard CRUD methods over SQLAlchemy tables.
  • Schema system based on Marshmallow that allows to declare the inputs and outputs of endpoints and provides a reliable way of validate data against those schemas.
  • Dependency Injection that ease the process of managing parameters needed in endpoints. Starlette ASGI objects like Request, Response, Session and so on are defined as components and ready to be injected in your endpoints.
  • Components as the base of the plugin ecosystem, allowing you to create custom or use those already defined in your endpoints, injected as parameters.
  • Auto generated API schema using OpenAPI standard. It uses the schema system of your endpoints to extract all the necessary information to generate your API Schema.
  • Auto generated docs providing a Swagger UI or ReDoc endpoint.
  • Pagination automatically handled using multiple methods such as limit and offset, page numbers...

Requirements

Installation

$ pip install starlette-api

Example

from marshmallow import Schema, fields, validate
from starlette_api.applications import Starlette


# Data Schema
class Puppy(Schema):
    id = fields.Integer()
    name = fields.String()
    age = fields.Integer(validate=validate.Range(min=0))


# Database
puppies = [
    {"id": 1, "name": "Canna", "age": 6},
    {"id": 2, "name": "Sandy", "age": 12},
]


# Application
app = Starlette(
    components=[],      # Without custom components
    title="Foo",        # API title
    version="0.1",      # API version
    description="Bar",  # API description
    schema="/schema/",  # Path to expose OpenAPI schema
    docs="/docs/",      # Path to expose Swagger UI docs
    redoc="/redoc/",    # Path to expose ReDoc docs
)


# Views
@app.route("/", methods=["GET"])
def list_puppies(name: str = None) -> Puppy(many=True):
    """
    description:
        List the puppies collection. There is an optional query parameter that 
        specifies a name for filtering the collection based on it.
    responses:
        200:
            description: List puppies.
    """
    return [puppy for puppy in puppies if puppy["name"] == name]
    

@app.route("/", methods=["POST"])
def create_puppy(puppy: Puppy) -> Puppy:
    """
    description:
        Create a new puppy using data validated from request body and add it 
        to the collection.
    responses:
        200:
            description: Puppy created successfully.
    """
    puppies.append(puppy)
    
    return puppy

Dependencies

Following Starlette philosophy Starlette API reduce the number of hard dependencies to those that are used as the core:

  • starlette - Starlette API is a layer on top of it.
  • marshmallow - Starlette API data schemas and validation.

It does not have any more hard dependencies, but some of them are necessaries to use some features:

  • pyyaml - Required for API Schema and Docs auto generation.
  • apispec - Required for API Schema and Docs auto generation.
  • python-forge - Required for pagination.
  • sqlalchemy - Required for Generic API resources.
  • databases - Required for Generic API resources.

You can install all of these with pip3 install starlette-api[full].

Credits

That library started as an adaptation of APIStar to work with Starlette, but a great amount of the code has been rewritten to use Marshmallow as the schema system.

Contributing

This project is absolutely open to contributions so if you have a nice idea, create an issue to let the community discuss it.

Metadata

Release files for starlette-api 0.7.0

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

Source distribution (sdist)

Source distribution for starlette-api 0.7.0
File Size Uploaded Provenance
starlette-api-0.7.0.tar.gz 38.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for starlette-api 0.7.0
File Interpreter ABI Platform
starlette_api-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 173.3 kB

Release files / starlette-api-0.7.0.tar.gz

Download URL starlette-api-0.7.0.tar.gz
Size 38.2 kB
Tags Source
SHA-256 checksum
How to use checksums
74859c8596bc35b12a306be3e1c38fa8e1f905ec213feb8d766f0300c57c638a
BLAKE2b-256 checksum
How to use checksums
60a2eacbfd5fe71c0a178db19d9abf65185ffbac38ff43286ab34e130ef8bd1b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.11 CPython/3.7.3 Linux/4.4.0-141-generic

Release files / starlette_api-0.7.0-py3-none-any.whl

Download URL starlette_api-0.7.0-py3-none-any.whl
Size 135.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1bac39dd89942d27f52c2a6cf00e35eea77c61c23c189248ce69379391e3666a
BLAKE2b-256 checksum
How to use checksums
39cfb6a9f4c698a902b4ff429f862ee84158a29527b0d8d4f41c1062a27758aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.11 CPython/3.7.3 Linux/4.4.0-141-generic

Security for starlette-api 0.7.0

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.

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