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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 84d3d52ce044087a0fd5225788d3cb9b1bab98e3327c8d610adca6828c46bc8b |
|
MD5 | 52ddb284d29ceead77d76571ac556980 |
|
BLAKE2b-256 | 68edee796906878faa104bf1d74fa810fd3dda490a887d3d5ee2b5efb5e1a188 |
Provenance
The following attestation bundles were made for fastapi_supabase-0.2.0.tar.gz
:
Publisher:
python-publish.yml
on aprilahijriyan/fastapi-supabase
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
fastapi_supabase-0.2.0.tar.gz
- Subject digest:
84d3d52ce044087a0fd5225788d3cb9b1bab98e3327c8d610adca6828c46bc8b
- Sigstore transparency entry: 151028217
- Sigstore integration time:
- Predicate type:
File details
Details for the file fastapi_supabase-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: fastapi_supabase-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea946395cfc40e1fc0475588423392b4b9f0ccd3ca86fd41889789c95abf6916 |
|
MD5 | 1ee030585ca18aaa66cb66cd67cf2d1d |
|
BLAKE2b-256 | b28765047fb34e555a82479552e273683105f0904864a73eb7cfdeb681e40edf |
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
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
fastapi_supabase-0.2.0-py3-none-any.whl
- Subject digest:
ea946395cfc40e1fc0475588423392b4b9f0ccd3ca86fd41889789c95abf6916
- Sigstore transparency entry: 151028219
- Sigstore integration time:
- Predicate type: