Skip to main content

Async Python SDK for Cloudflare D1 Databases

Project description

D1py

A modern, async Python client for the Cloudflare D1 REST API.

D1 is Cloudflare's serverless SQLite-based database. D1py gives you full programmatic access to manage databases, run queries, import/export data, and use time-travel — all from Python with first-class async support.

Highlights

  • Async-first — built on httpx.AsyncClient with HTTP/2 and connection pooling for high throughput
  • Full D1 API — every endpoint covered: database CRUD, queries, batches, export/import, time travel
  • Type-safe — Pydantic v2 models for all request params and responses, full IDE autocomplete
  • Sync fallback — every method has a *_sync() variant for scripts and notebooks
  • Automatic retries — exponential backoff on 5xx and network errors, configurable
  • Context manager — clean resource lifecycle with async with

Install

pip install cfd1py

From source:

git clone https://github.com/Suleman-Elahi/D1py && cd D1py
pip install .

Requires Python 3.10+. Dependencies: httpx, pydantic.

Quick Example

import asyncio
from D1py import D1py

async def main():
    async with D1py(account_id="xxx", api_token="xxx") as d1:
        db = await d1.create_database("my-app")
        await d1.query(db.uuid, "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")
        await d1.query(db.uuid, "INSERT INTO users (name) VALUES (?)", params=["Alice"])
        results = await d1.query(db.uuid, "SELECT * FROM users")
        print(results[0].results)

asyncio.run(main())

Documentation

Guide What it covers
Getting Started Installation, authentication, first query
Database Management Create, list, get, update, delete databases
Queries Single queries, parameterized queries, batch queries, raw queries
Export & Import Export databases to SQL, import SQL files
Time Travel Bookmarks, restore to point-in-time
Configuration Client options, timeouts, retries, connection pooling
Error Handling Exception types, error codes, retry behavior
Async vs Sync When to use async, sync wrappers, patterns

API At a Glance

Database management: list_databases, create_database, get_database, update_database, patch_database, delete_database

Queries: query, query_batch, raw_query, raw_query_batch

Data ops: export_database, import_database

Time travel: get_bookmark, restore

Every async method has a sync counterpart (e.g. query_sync, create_database_sync).

License

MIT

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

cfd1py-2.0.0.tar.gz (48.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cfd1py-2.0.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file cfd1py-2.0.0.tar.gz.

File metadata

  • Download URL: cfd1py-2.0.0.tar.gz
  • Upload date:
  • Size: 48.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for cfd1py-2.0.0.tar.gz
Algorithm Hash digest
SHA256 d6bcd843643c448ce6e81841bdcd6e2b5743d78355ad13de9affa78054ae3b38
MD5 b612fc7ec5f055efa1f8e6dc31b85f84
BLAKE2b-256 b5fe962c5865a5f04c419be936a256d84afef2a9e8074e400ea3311564eec4ad

See more details on using hashes here.

File details

Details for the file cfd1py-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: cfd1py-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for cfd1py-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5c5069326da341d27a65196e34c6ff2ca766a21f3109b24a521d472df252db41
MD5 16f2ff6c855d25af8a1c2b555084eea8
BLAKE2b-256 9a567e82e686777e65864bebc55b622680505ec5f65d5cc0857f0fa6f8028486

See more details on using hashes here.

Supported by

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