Skip to main content

civicrm-py

CI PyPI version Python Version License: MIT

A Python client for CiviCRM's API v4. Async by default, with a query interface that'll feel familiar if you've used Django's ORM.

What it does

  • Async HTTP via httpx (sync wrappers available)
  • Type-safe models using msgspec
  • Chainable queries: client.Contact.filter(is_deleted=False).limit(10).all()
  • Integrations for Django, Litestar, FastAPI, Flask, and others

Install

pip install civicrm-py

With a framework:

pip install civicrm-py[django]
pip install civicrm-py[litestar]
pip install civicrm-py[fastapi]

Optional extras:

pip install civicrm-py[sqlspec]    # Local database caching
pip install civicrm-py[workflows]  # Workflow automation

See docs for sqlspec, workflows, and pytest-databases.

Setup

export CIVI_BASE_URL=https://your-site.org/civicrm/ajax/api4
export CIVI_API_KEY=your-api-key
export CIVI_SITE_KEY=your-site-key

Usage

import asyncio
from civicrm_py import CiviClient

async def main():
    async with CiviClient() as client:
        # Get all active contacts
        contacts = await client.Contact.filter(is_deleted=False).all()

        # Single contact by ID
        contact = await client.Contact.get(id=123)

        # Create
        new_contact = await client.Contact.create(
            first_name="John",
            last_name="Doe",
            contact_type="Individual",
        )

        # Update
        await client.Contact.update(id=123, first_name="Jane")

        # Delete
        await client.Contact.delete(id=123)

asyncio.run(main())

Queries

# Select specific fields, filter, sort, paginate
contacts = await (
    client.Contact
    .select("id", "display_name", "email_primary.email")
    .filter(is_deleted=False, contact_type="Individual")
    .order_by("-created_date")
    .limit(25)
    .all()
)

# Operators
contacts = await (
    client.Contact
    .filter_by("created_date", ">=", "2024-01-01")
    .filter_by("display_name", "LIKE", "%Smith%")
    .all()
)

# Count and exists
total = await client.Contact.filter(is_deleted=False).count()
exists = await client.Contact.filter(id=123).exists()

Litestar

from litestar import Litestar, get
from civicrm_py import CiviClient
from civicrm_py.contrib.litestar import CiviPlugin

@get("/contacts")
async def get_contacts(civi: CiviClient) -> list[dict]:
    return await civi.Contact.filter(is_deleted=False).all()

app = Litestar(
    route_handlers=[get_contacts],
    plugins=[CiviPlugin()],
)

Django

# settings.py
CIVI_SETTINGS = {
    "BASE_URL": "https://example.org/civicrm/ajax/api4",
    "API_KEY": env("CIVI_API_KEY"),
    "SITE_KEY": env("CIVI_SITE_KEY"),
}

# views.py
from civicrm_py.contrib.django import get_civi_client

async def contact_list(request):
    async with get_civi_client() as client:
        contacts = await client.Contact.all()
        return JsonResponse({"contacts": contacts})

Development

git clone https://github.com/JacobCoffee/civicrm-py.git
cd civicrm-py

make install  # dependencies
make test     # tests
make check    # lint + typecheck

Docs

civi.scriptr.dev

Alternatives

  • civipy - Another Python client for CiviCRM. No built-in web framework integrations.

License

MIT

Metadata

Release files for civicrm-py 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for civicrm-py 0.1.3
File Size Uploaded Provenance Actions
civicrm_py-0.1.3.tar.gz 211.1 kB With provenance Details for civicrm_py-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for civicrm-py 0.1.3
File Interpreter ABI Platform Actions
civicrm_py-0.1.3-py3-none-any.whl Python 3 none any Details for civicrm_py-0.1.3-py3-none-any.whl

Total release size: 472.3 kB

Release files / civicrm_py-0.1.3.tar.gz

Download URL civicrm_py-0.1.3.tar.gz
Size 211.1 kB
Tags Source
SHA-256 checksum
How to use checksums
4389db63dd10550f53d30c0a03947e1d1309d27e357291b47203d206fa6c467f
BLAKE2b-256 checksum
How to use checksums
dd6a3a4d5f588c02d71e8f9e47868a71ef0bcde66d9ab2261786e7ae60224e0e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 11, 2026.

Transparency log

Release files / civicrm_py-0.1.3-py3-none-any.whl

Download URL civicrm_py-0.1.3-py3-none-any.whl
Size 261.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
09cf6d52d3fd5d53fcd3eb3efcbc3007ef2d2e100617ae7f2c0478f197ce3d88
BLAKE2b-256 checksum
How to use checksums
ccd87c446b840248e6d33d07f6e9a3c427bb92d791c18c172b30a3f85f44793a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 11, 2026.

Transparency log

Security for civicrm-py 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for civicrm_py-0.1.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 11, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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