Skip to main content

Integrate supabase to FastAPI with less effort

Project description

fastapi-supabase

Integrate supabase to FastAPI with less effort

Installation

pip install fastapi-supabase

For local development:

uv sync
uv pip install -e .

Usage

This module needs to initialize the Supabase client object during the lifespan event. You can do it like this:

SUPABASE_URL = os.environ["SUPABASE_URL"]
SUPABASE_KEY = os.environ["SUPABASE_KEY"]


@asynccontextmanager
async def lifespan(app: FastAPI):
    yield await fastapi_supabase.lifespan(SUPABASE_URL, SUPABASE_KEY)

app = FastAPI(
    title="FastAPI Supabase",
    lifespan=lifespan # Here we add a function to handle when the app starts up.
)
# You're all set!

We provide some dependency functions, which help you access supabase client on FastAPI endpoints.

SupabaseClient

This type helps you access supabase asynchronous clients.

Example usage:

@app.post("/login")
async def login(sp: SupabaseClient, form_data: Annotated[OAuth2PasswordRequestForm, Depends()]):
    """
    Login and get access token
    """
    # login using email
    data = {
        "email": form_data.username,
        "password": form_data.password
    }
    resp = await sp.auth.sign_in_with_password(data)
    if resp.user:
        access_token = None
        if resp.session:
            access_token = resp.session.access_token
        
        return {"access_token": access_token}
    raise HTTPException(400, "Invalid username or password")

SupabaseSession

This type is same as SupabaseClient. The difference is this type adds access token to supabase client automatically and endpoint will be protected.

CurrentUser

This type is to get the current user object. And, if you use this type, your endpoint will not be accessible if the client request does not provide a Bearer access token. The access token must be valid on the supabase.

Example:

@app.get("/book")
async def list_book(sp: SupabaseSession, current_user: CurrentUser, page: int = 1, limit: int = 10):
    """
    List of books with pagination.
    """
    tbl = sp.table("books")
    offset = (page - 1) * limit
    resp = await tbl.select("*").eq("user_id", current_user.id).limit(limit).offset(offset).execute()
    return resp.data

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_supabase-0.2.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

fastapi_supabase-0.2.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_supabase-0.2.0.tar.gz.

File metadata

  • Download URL: fastapi_supabase-0.2.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for fastapi_supabase-0.2.0.tar.gz
Algorithm Hash digest
SHA256 84d3d52ce044087a0fd5225788d3cb9b1bab98e3327c8d610adca6828c46bc8b
MD5 52ddb284d29ceead77d76571ac556980
BLAKE2b-256 68edee796906878faa104bf1d74fa810fd3dda490a887d3d5ee2b5efb5e1a188

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_supabase-0.2.0.tar.gz:

Publisher: python-publish.yml on aprilahijriyan/fastapi-supabase

Attestations:

File details

Details for the file fastapi_supabase-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_supabase-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ea946395cfc40e1fc0475588423392b4b9f0ccd3ca86fd41889789c95abf6916
MD5 1ee030585ca18aaa66cb66cd67cf2d1d
BLAKE2b-256 b28765047fb34e555a82479552e273683105f0904864a73eb7cfdeb681e40edf

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_supabase-0.2.0-py3-none-any.whl:

Publisher: python-publish.yml on aprilahijriyan/fastapi-supabase

Attestations:

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page