Getting Started#

We connect to the API and authenticate, and fetch some data.

 1import asyncio
 2from postgrest import AsyncPostgrestClient
 3
 4async def main():
 5    async with AsyncPostgrestClient("http://localhost:3000") as client:
 6        client.auth("Bearer <token>")
 7        r = await client.from_("countries").select("*").execute()
 8        countries = r.data
 9
10asyncio.run(main())

CRUD

await client.from_("countries").insert({ "name": "Việt Nam", "capital": "Hà Nội" }).execute()
r = await client.from_("countries").select("id", "name").execute()
countries = r.data
await client.from_("countries").update({"capital": "Hà Nội"}).eq("name", "Việt Nam").execute()
await client.from_("countries").delete().eq("name", "Việt Nam").execute()

Calling RPCs

await client.rpc("foo").execute()
await client.rpc("bar", {"arg1": "value1", "arg2": "value2"}).execute()

Closing the connection

Once you have finished running your queries, close the connection:

await client.aclose()

You can also use the client with a context manager, which will close the client for you.

async with AsyncPostgrestClient("url") as client:
    # run queries
# the client is closed when the async with block ends