CalDAV Client Library

3.1.0 · active · verified Thu Apr 16

caldav is a Python client library for the CalDAV (RFC4791) protocol, enabling interaction with CalDAV servers to manage calendars, events, and tasks. Version 3.1.0 focuses on robustness, improved async support, and enhanced multi-server capabilities. The library maintains an active development cycle, frequently releasing bug fixes and server compatibility improvements, with major releases occurring every few years and minor releases more often.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a CalDAV server, authenticate, retrieve the user's principal, list available calendars, and fetch the first event from each calendar. Credentials can be provided directly or via environment variables for security.

import os
from caldav.davclient import DAVClient

# Replace with your CalDAV server details or use environment variables
CALDAV_URL = os.environ.get('CALDAV_URL', 'http://localhost:8008/dav.php/')
CALDAV_USER = os.environ.get('CALDAV_USER', 'user1')
CALDAV_PASS = os.environ.get('CALDAV_PASS', 'user1')

def get_calendars_sync():
    print(f"Connecting to CalDAV server at {CALDAV_URL} as {CALDAV_USER}")
    try:
        with DAVClient(url=CALDAV_URL, username=CALDAV_USER, password=CALDAV_PASS) as client:
            # Get the principal (user's root DAV resource)
            principal = client.principal()
            print(f"Connected to principal: {principal.url}")

            # Get all calendars for the principal
            calendars = principal.calendars()
            if not calendars:
                print("No calendars found.")
                return

            print(f"Found {len(calendars)} calendar(s):")
            for calendar in calendars:
                print(f"- {calendar.name} (URL: {calendar.url})")
                # Example: Fetch first event from the first calendar (if any)
                events = calendar.events()
                if events:
                    event = events[0]
                    print(f"  First event: {event.summary} (UID: {event.uid})")
                else:
                    print("  No events found in this calendar.")

    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    get_calendars_sync()

view raw JSON →