Dapr Python SDK

1.17.4 · active · verified Thu Apr 16

The Dapr Python SDK (version 1.17.4) is the official library for building distributed applications with Dapr building blocks in Python. Dapr provides a portable, event-driven, serverless runtime that simplifies microservices development across cloud and edge environments. The SDK allows interaction with Dapr's APIs for state management, pub/sub, service invocation, bindings, and more. Dapr generally releases minor updates every 6-13 weeks, with three recent minor versions typically supported concurrently.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Dapr Python client and interact with the Dapr state management building block to save and retrieve a key-value pair. Ensure a Dapr sidecar is running and a state store component (e.g., 'statestore') is configured and accessible to your application.

import os
from dapr.clients import DaprClient

STATE_STORE_NAME = os.environ.get('STATE_STORE_NAME', 'statestore')

def main():
    with DaprClient() as client:
        key = 'my_key'
        value = 'my_value'

        # Save state
        print(f"Saving state: key={key}, value={value}")
        client.save_state(
            store_name=STATE_STORE_NAME,
            key=key,
            value=value
        )
        print("State saved successfully.")

        # Get state
        print(f"Getting state for key: {key}")
        response = client.get_state(
            store_name=STATE_STORE_NAME,
            key=key
        )
        retrieved_value = response.data.decode('utf-8')
        print(f"Retrieved state: value={retrieved_value}")

        assert retrieved_value == value
        print("State retrieval verified.")

if __name__ == '__main__':
    # Make sure a Dapr sidecar is running (e.g., `dapr run --app-id myapp --dapr-http-port 3500 --dapr-grpc-port 50001 python your_script.py`)
    # and a state store component named 'statestore' is configured.
    main()

view raw JSON →