Skip to main content

FaastAPI

Easily create OpenFaas functions built on top of FastAPI Python framework

Warning:

This project is in its early phase. It might be subject to a lot of changes and many features might be added/removed.

Overview

FaastAPI is built on top of FastAPI framework, pydantic library, and jinja2 library.

It allows developers to quickly generate OpenFaas functions.

Several plugins can be used to quicly set up a rest API. A command line interface can be installed with the package so that functions can be tested locally.

Installation

You can install package using pip:

pip install faastapi

By default, the command line interface and plugins are not installed. You can choose to install the cli using the cli extra:

pip install faastapi[cli]

Example

A simple Faastapi function file (by default Faastapi search files named faastapi.yml) can be as follow:

---
apiversion: v1

meta:
  name: hello-faastapi
  version: 0.3.4

  image: gcharbon/hello-fastapi
  tags:
    - test
    - demo
  description: A dummy function to demonstrate Faastapi

build:
  plugins:
    basic-auth:
      username: admin
      password: password
    mongodb:
      host:  127.0.0.1
      port: 27017
      database: demo
    sqlite:
      uri: sqlite:///example.db
    async-sqlite:
    instrumentation:

run:
  script: hello.py
  function: say_hello
  method: post
  input:
    name: 
      type: str
      default: World
  output:
    message: str
    date: datetime
    drivers: Dict[str,str]

Considering the file hello.py is present next to faastapi.yml with the following content:

import datetime


def say_hello(context, name):
    return {
        "message": f"Hello {name}",
        "date": datetime.datetime.now(),
        "drivers": {
            "mongodb": str(context.mongodb),
            "sqlite": str(context.sql),
            "async-sqlite": str(context.async_sql),
        },
    }

Note: As you can see function must always accept an argument named context. In this case, since sqlite, async-sqlite and mongodb plugins where specified, clients are available in the context.

When the user run faastapi function new -f faastapi.yml openfaas, an OpenFaas function will be generated in the openfaas directory. This function can then be deployed to OpenFaas with faas-cli:

cd openfaas/
faas-cli up -f function.yml

You can then try the function on OpenFaas portal (http://<OPENFAAS_URL>):

Screenshot of OpenFaas portal

You can also access the documentation of the function at http://<OPENFAAS_URL>/function/hello-faastapi/docs:

Screenshot of documentation

List of available plugins

Security plugins:

  • basic-auth: Enables a basic authentication in your application.

Note: Username and password can be configured as in the above example

  • oauth2-password: Enable simple oauth2 with password and bearer.

Note: At this moment, configuration of user database is not possible, thus usage is limited to default values (username: admin, password: secret)

Databases plugins:

  • sqlite: Injects an sqlalchemy Session with sqlite3 engine into the context by default.
  • postgres: Injects an sqlachemy Session with psycopg2 engine into the context by default.
  • mysql: Injects an sqlalchemy Session with mysqlclient engine into the context by default.
  • async-sqlite: Injects a databases.Database instance with aiosqlite engine into the context by default.
  • async-postgres: Injects a databases.Database instance with asyncpg engine into the context by default.
  • async-mysql: Injects a databases.Database instance with aiomysql engine into the context by default.

Note: All those plugins accept a single configuration variable: uri

  • mongodb: Injects a pymongo.Database instance into the context.

Note: MongoDB plugin can be configured using the variables: host (default to 127.0.0.1), port (default to 27017) and database (default to test)

  • redis: Injects a Redis instance into the context.

Note: Redis plugin can be configured using the variables: host (default to 127.0.0.1), port (default to 6379) and db (default to 0)

Metadata

Release files for faastapi 0.3.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 faastapi 0.3.4
File Size Uploaded
faastapi-0.3.4.tar.gz 18.7 kB Details

Built distribution (wheel)

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

Total release size: 43.5 kB

Release files / faastapi-0.3.4.tar.gz

Download URL faastapi-0.3.4.tar.gz
Size 18.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0a850c9c015774aab19de093cf44a3c1a7f69f09b7cc1eda507964b983645ee3
BLAKE2b-256 checksum
How to use checksums
59b48c77850f82c8abc2c09d7adddc3f6c4c23a3115fe2257f7c225798b08c16
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.16 CPython/3.7.4 Linux/4.4.0-43-Microsoft

Release files / faastapi-0.3.4-py3-none-any.whl

Download URL faastapi-0.3.4-py3-none-any.whl
Size 24.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
959fa6afffdc3149e42c5941204f8ad45e77ddabc6744d1c674ee0738505a2c2
BLAKE2b-256 checksum
How to use checksums
4b1cb50872a23668f3b50906c02be26ab002846ab3b3e52d0fcb20b962a96e27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.16 CPython/3.7.4 Linux/4.4.0-43-Microsoft

Release history Release notifications | RSS feed

This release

0.3.4 This release

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

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