Skip to main content
The Pragmatiks SDK provides Python clients for managing resources programmatically. It offers both synchronous and asynchronous clients, typed responses, and utilities for building providers.

Installation

Quick Start

The client auto-discovers credentials from pragma auth login or the environment. Resource operations live on a project handle:
To create or update resources from YAML, use pragma resources apply.

Authentication

The SDK discovers credentials in this order:
  1. Explicit token - Pass auth_token to the client
  2. Context-specific environment variable - PRAGMA_AUTH_TOKEN_{CONTEXT} (uppercase)
  3. Generic environment variable - PRAGMA_AUTH_TOKEN
  4. CLI credentials file - Tokens stored by pragma auth login

Contexts

Contexts switch between environments, such as development and production. PragmaClient(context="production") reads PRAGMA_AUTH_TOKEN_PRODUCTION or the credentials file. Set the default context via environment variable:

PragmaClient

Synchronous client for the Pragmatiks API. Use as a context manager to ensure connections are closed.

Constructor

Context Manager

The with block closes the connection on exit. Without it, call close():

Health Check

Check if the API is reachable and healthy.

Get Current User

Get information about the authenticated user.
Returns: UserInfo with user_id, email, organization_id, and organization_name. Raises: httpx.HTTPStatusError if not authenticated (401).

list_resource_schemas

List the resource types that installed providers register.
Returns: List of ResourceSchema with provider, resource, config_schema, outputs_schema, and description.

Resource Operations

client.project(project_id) returns a ProjectResources handle. Every resource operation is scoped to that project.

list_resources

List resources, filtered by provider, resource type, and tags (a resource must match every tag). Returns dictionaries by default, or instances of model. reveal=True includes sensitive values.

get_resource

Get a single resource by its identifier.
Raises: httpx.HTTPStatusError if the resource is not found (404).

apply_resource

Create a resource or update an existing one. The resource’s project_id must match the handle’s project. Raises: ProjectMismatchError if resource.project_id differs from the handle’s project; httpx.HTTPStatusError if the API rejects the resource.

deactivate_resource

Tear down the resource’s external object and return the resource to draft. dry_run=True previews the teardown without changing anything.

delete_resource

Delete a resource. The resource enters the deletion lifecycle and is removed after cleanup.
Returns: TeardownResponse with lifecycle_state, impact (every resource the teardown reaches, the requested one first), and dry_run.

wait_ready, wait_deactivated, wait_deleted

Poll a resource until it reaches ready. wait_deactivated waits for draft and wait_deleted for deleted, with the same parameters. timeout=0 or None waits forever. Raises: ResourceFailedError if the resource reaches failed; TimeoutError if the timeout expires first.

Dead Letter Operations

Dead letter events are failed lifecycle events that can be retried or discarded.

list_dead_letter_events

List failed events awaiting manual intervention, optionally for one provider.

get_dead_letter_event

Get details of a specific dead letter event.

retry_dead_letter_event

Retry a single failed event.

retry_all_dead_letter_events

Retry all dead letter events. Returns the count of events retried.

delete_dead_letter_event

Discard a dead letter event without retrying.

delete_dead_letter_events

Bulk delete dead letter events for one provider, or every event with all=True. Returns the count deleted.
Raises: ValueError if neither provider nor all=True is specified.

Provider Operations

These methods back the pragma providers commands; see the CLI reference for the workflow. Provider names are namespaced as org/name.
DeploymentResult.status is one of pending, progressing, available, or failed.

AsyncPragmaClient

Asynchronous client with the same API as PragmaClient. All methods are async, and client.project(project_id) returns an AsyncProjectResources handle.

Async Context Manager

The async with block closes the connection on exit. Without it, await close():

Concurrent Operations

Run independent operations concurrently:

Lifecycle Events

AsyncPragmaClient.stream_lifecycle_events(timeout=None) yields one frame per resource lifecycle transition in your organization. timeout=0 or None streams until you stop consuming.

Typed Responses

Pass a provider’s resource class as model to get typed instances back. The response’s config and outputs are validated against that class, so use the class from the installed provider version:

Lifecycle States

LifecycleState is a string enum, so it compares equal to the lifecycle_state string in a dictionary response:

Error Handling

The SDK raises httpx.HTTPStatusError for API errors. Handle common cases:

Authentication Errors

PragmaClient(require_auth=True) raises ValueError when no credentials are found. Run pragma auth login or set PRAGMA_AUTH_TOKEN.

Timeout Handling

Requests that exceed the client’s timeout raise httpx.TimeoutException. Raise the limit for slow calls:

File Uploads

Upload files to Pragmatiks storage programmatically:
Returns: A dictionary with url, public_url, size, content_type, checksum, and uploaded_at.

Dependencies and Field References

A resource config points at another resource in one of two ways:
  • Field reference: a single output value, written in YAML as provider, resource, name, and field. See Resources.
  • Dependency[T]: a whole resource, with typed access to its config and outputs in provider code.
Owner references, which make a child resource deleted along with its owner, are described in Resources.

Dependency[T]

Provider authors declare a whole-resource dependency with Dependency[T]. The platform waits for the dependency to be ready before it runs the dependent’s handlers, and resolve() returns it:

Provider Authoring

The SDK also provides utilities for building providers. See Building Providers for complete documentation.

Key Exports

pragma_sdk exports the building blocks of a resource: the Resource, Config, and Outputs base classes, the Provider registry, the field types, and HealthStatus.

Quick Example


Next Steps

Building Providers

Create custom providers with lifecycle methods.

Lifecycle Methods

Deep dive into on_observe, on_create, on_update, and on_delete.

API Reference

Full REST API documentation.

Error Recovery

Handle failures and retry dead letter events.