campai-api 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- campai_api-0.1.0/PKG-INFO +344 -0
- campai_api-0.1.0/README.md +333 -0
- campai_api-0.1.0/pyproject.toml +159 -0
- campai_api-0.1.0/pyproject.toml.orig +162 -0
- campai_api-0.1.0/src/campai_api/__init__.py +182 -0
- campai_api-0.1.0/src/campai_api/async_client.py +148 -0
- campai_api-0.1.0/src/campai_api/auth.py +49 -0
- campai_api-0.1.0/src/campai_api/client.py +144 -0
- campai_api-0.1.0/src/campai_api/exceptions.py +294 -0
- campai_api-0.1.0/src/campai_api/models/__init__.py +95 -0
- campai_api-0.1.0/src/campai_api/models/auth.py +91 -0
- campai_api-0.1.0/src/campai_api/models/finance.py +239 -0
- campai_api-0.1.0/src/campai_api/models/members.py +257 -0
- campai_api-0.1.0/src/campai_api/models/webhooks.py +32 -0
- campai_api-0.1.0/src/campai_api/resources/__init__.py +26 -0
- campai_api-0.1.0/src/campai_api/resources/auth.py +134 -0
- campai_api-0.1.0/src/campai_api/resources/base.py +46 -0
- campai_api-0.1.0/src/campai_api/resources/finance.py +226 -0
- campai_api-0.1.0/src/campai_api/resources/members.py +252 -0
- campai_api-0.1.0/src/campai_api/resources/webhooks.py +116 -0
- campai_api-0.1.0/src/campai_api/serialization.py +426 -0
- campai_api-0.1.0/src/campai_api/transport.py +642 -0
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: campai-api
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Idiomatic, type-safe Python SDK for the Campai REST API.
|
|
5
|
+
Author: Stephan Lukasczyk
|
|
6
|
+
Author-email: Stephan Lukasczyk <stephan@dante.de>
|
|
7
|
+
License-Expression: EUPL-1.2
|
|
8
|
+
Requires-Dist: httpx>=0.27.0
|
|
9
|
+
Requires-Python: >=3.12
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
|
|
12
|
+
<!--
|
|
13
|
+
SPDX-FileCopyrightText: 2026 Stephan Lukasczyk <stephan@dante.de>
|
|
14
|
+
|
|
15
|
+
SPDX-License-Identifier: EUPL-1.2
|
|
16
|
+
-->
|
|
17
|
+
|
|
18
|
+
# campai-api
|
|
19
|
+
|
|
20
|
+
Idiomatic, lightweight, and type-safe Python SDK for the [Campai REST API](https://cloud.campai.com/api).
|
|
21
|
+
|
|
22
|
+
[](https://www.python.org/downloads/)
|
|
23
|
+
[](https://github.com/astral-sh/ty)
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## Features
|
|
28
|
+
|
|
29
|
+
- **Minimal Dependencies**: Strictly limited to a single runtime dependency (`httpx>=0.27.0`).
|
|
30
|
+
- **Dual Native Clients**: Synchronous (`CampaiClient`) and asynchronous (`AsyncCampaiClient`) with native context
|
|
31
|
+
managers (`with` / `async with`) and explicit lifecycle methods (`close()` / `aclose()`).
|
|
32
|
+
- **Domain-Scoped Namespaces**: Logical grouping of endpoints under `client.auth`, `client.members`, `client.finance`,
|
|
33
|
+
and `client.webhooks`.
|
|
34
|
+
- **Pure Slotted Dataclasses**: Response and request structures built with standard library
|
|
35
|
+
`@dataclass(slots=True, kw_only=True)` for lightweight performance and full autocompletion.
|
|
36
|
+
- **Accurate OpenAPI Contracts**: Built against Campai's live OpenAPI specification (`https://cloud.campai.com/api`),
|
|
37
|
+
preserving exact payload constraints, integer cent amounts, and path structures.
|
|
38
|
+
- **Typed Error Hierarchy**: Clear exception mapping for HTTP status codes (e.g., `AuthenticationError` for 401,
|
|
39
|
+
`NotFoundError` for 404, `RateLimitError` for 429).
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## Installation
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
# Using uv (recommended)
|
|
47
|
+
uv add campai-api
|
|
48
|
+
|
|
49
|
+
# Using pip
|
|
50
|
+
pip install campai-api
|
|
51
|
+
|
|
52
|
+
# Install from source / Git repository
|
|
53
|
+
pip install git+https://codeberg.org/stephanlukasczyk/campai-api.git
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## Supported Endpoints & Coverage
|
|
59
|
+
|
|
60
|
+
Campai's OpenAPI specification defines ~664 API paths. This SDK provides complete, high-level typed bindings for the
|
|
61
|
+
core business resources:
|
|
62
|
+
|
|
63
|
+
| Domain Namespace | Endpoints & Routes | Supported Capabilities |
|
|
64
|
+
|:------------------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------------------------------------------------------------------------------------------|
|
|
65
|
+
| `client.auth` | `GET /apiKey/info`<br>`GET /organizations/{orgId}`<br>`POST /organizations/{orgId}/mandates/list`<br>`GET /organizations/{orgId}/mandates/{mandateId}` | API key capability lookup, organization details, mandate listing, and retrieval. |
|
|
66
|
+
| `client.members` | `POST /{orgId}/{mandateId}/crm/contacts/list`<br>`POST /{orgId}/{mandateId}/crm/contacts`<br>`GET /{orgId}/{mandateId}/crm/contacts/{id}`<br>`POST /{orgId}/{mandateId}/crm/contacts/{id}`<br>`DELETE /{orgId}/{mandateId}/crm/contacts/{id}` | CRM contact & member search, pagination, full creation, sparse/partial updates, and deletion. |
|
|
67
|
+
| `client.finance` | `GET /{orgId}/finance/settings`<br>`POST /{orgId}/finance/settings`<br>`POST /{orgId}/{mandateId}/finance/receipts/list`<br>`POST /{orgId}/{mandateId}/receipts/invoice`<br>`POST /{orgId}/{mandateId}/receipts/invoice/{id}` | Financial settings, receipt queries/pagination, and invoice creation & updates (amounts in cents). |
|
|
68
|
+
| `client.webhooks` | `POST /{orgId}/oldContacts/{id}/triggerWebhooks`<br>`POST /{orgId}/oldMembers/{id}/triggerWebhooks` | Triggering webhook dispatches (`created`, `updated`, `deleted`) for contacts and members. |
|
|
69
|
+
|
|
70
|
+
> **Note**: For specialised or legacy endpoints outside these core modules, the underlying transport engine
|
|
71
|
+
> (`client.transport.request(...)`) can be used directly with full authentication and error mapping.
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
## Quick Start
|
|
76
|
+
|
|
77
|
+
### Synchronous Client (`CampaiClient`)
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from campai_api import (
|
|
81
|
+
Address,
|
|
82
|
+
CampaiClient,
|
|
83
|
+
ContactCreateRequest,
|
|
84
|
+
InvoiceCreateRequest,
|
|
85
|
+
InvoicePositionRequest,
|
|
86
|
+
PersonCreateInput,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
with CampaiClient(api_key="your_campai_api_key") as client:
|
|
90
|
+
# 1. Retrieve API key and organisation details
|
|
91
|
+
info = client.auth.get_info()
|
|
92
|
+
org_id = info.organization
|
|
93
|
+
print(f"Authenticated with organisation: {org_id}")
|
|
94
|
+
|
|
95
|
+
# 2. Retrieve active mandates
|
|
96
|
+
mandates_resp = client.auth.list_mandates(org_id)
|
|
97
|
+
mandate_id = mandates_resp.mandates[0].id
|
|
98
|
+
|
|
99
|
+
# 3. List CRM contacts / members
|
|
100
|
+
contacts_resp = client.members.list(
|
|
101
|
+
organization_id=org_id,
|
|
102
|
+
mandate_id=mandate_id,
|
|
103
|
+
limit=20,
|
|
104
|
+
search_term="Mustermann",
|
|
105
|
+
)
|
|
106
|
+
for contact in contacts_resp.contacts:
|
|
107
|
+
print(f"Contact: {contact.name} ({contact.id})")
|
|
108
|
+
|
|
109
|
+
# 4. Create a new contact
|
|
110
|
+
new_contact = client.members.create(
|
|
111
|
+
organization_id=org_id,
|
|
112
|
+
mandate_id=mandate_id,
|
|
113
|
+
contact=ContactCreateRequest(
|
|
114
|
+
type="person",
|
|
115
|
+
person=PersonCreateInput(
|
|
116
|
+
title="Dr.",
|
|
117
|
+
first_name="Erika",
|
|
118
|
+
last_name="Mustermann",
|
|
119
|
+
gender=None,
|
|
120
|
+
birthdate=None,
|
|
121
|
+
),
|
|
122
|
+
),
|
|
123
|
+
)
|
|
124
|
+
print(f"Created contact ID: {new_contact.id}")
|
|
125
|
+
|
|
126
|
+
# 5. Create an invoice in the finance module (amounts in cents)
|
|
127
|
+
invoice_result = client.finance.create_invoice(
|
|
128
|
+
organization_id=org_id,
|
|
129
|
+
mandate_id=mandate_id,
|
|
130
|
+
invoice=InvoiceCreateRequest(
|
|
131
|
+
draft=False,
|
|
132
|
+
address=Address(
|
|
133
|
+
address_line="Musterstr. 1",
|
|
134
|
+
zip="10115",
|
|
135
|
+
city="Berlin",
|
|
136
|
+
country="DE",
|
|
137
|
+
),
|
|
138
|
+
delivery_date_type="delivery",
|
|
139
|
+
title="Annual Membership Fee 2026",
|
|
140
|
+
intro="Thank you for being a valued member.",
|
|
141
|
+
account=8400,
|
|
142
|
+
receipt_date="2026-01-01",
|
|
143
|
+
due_date="2026-01-31",
|
|
144
|
+
positions=[
|
|
145
|
+
InvoicePositionRequest(
|
|
146
|
+
description="Annual Membership Fee",
|
|
147
|
+
unit_amount=12000, # 120.00 EUR in integer cents
|
|
148
|
+
account=8400,
|
|
149
|
+
quantity=1.0,
|
|
150
|
+
)
|
|
151
|
+
],
|
|
152
|
+
),
|
|
153
|
+
)
|
|
154
|
+
print(f"Created invoice receipt ID: {invoice_result.id}")
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Asynchronous Client (`AsyncCampaiClient`)
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
import asyncio
|
|
161
|
+
from campai_api import AsyncCampaiClient
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
async def main() -> None:
|
|
165
|
+
async with AsyncCampaiClient(api_key="your_campai_api_key") as client:
|
|
166
|
+
# Retrieve organisation and mandates
|
|
167
|
+
info = await client.auth.get_info()
|
|
168
|
+
mandates_resp = await client.auth.list_mandates(info.organization)
|
|
169
|
+
mandate_id = mandates_resp.mandates[0].id
|
|
170
|
+
|
|
171
|
+
# Async list contacts
|
|
172
|
+
response = await client.members.list(
|
|
173
|
+
organization_id=info.organization,
|
|
174
|
+
mandate_id=mandate_id,
|
|
175
|
+
limit=50,
|
|
176
|
+
)
|
|
177
|
+
print(f"Found {response.count} contacts.")
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
asyncio.run(main())
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
## Architecture & Important API Concepts
|
|
186
|
+
|
|
187
|
+
### 1. Default Server URL & Authentication
|
|
188
|
+
|
|
189
|
+
- **Default Base URL**: `https://cloud.campai.com/api` (the `/api` path prefix is automatically preserved and validated
|
|
190
|
+
against path-traversal attacks).
|
|
191
|
+
- **Authentication**: All API requests inject the `X-API-Key` header with your token.
|
|
192
|
+
|
|
193
|
+
### 2. Organization and Mandate Scoping
|
|
194
|
+
|
|
195
|
+
Campai operates on a multi-tenant structure where CRM contacts, receipts, and invoices are partitioned under both an
|
|
196
|
+
**organisation ID** and a **mandate ID**:
|
|
197
|
+
|
|
198
|
+
- Obtain your organisation ID via `client.auth.get_info().organization`.
|
|
199
|
+
- List available mandates via `client.auth.list_mandates(org_id)` or fetch a specific mandate with
|
|
200
|
+
`client.auth.get_mandate(org_id, mandate_id)`.
|
|
201
|
+
- Pass `organization_id` and `mandate_id` as path arguments to scoped operations in `client.members` and
|
|
202
|
+
`client.finance`.
|
|
203
|
+
|
|
204
|
+
### 3. List Operations are POST Endpoints
|
|
205
|
+
|
|
206
|
+
In the Campai REST API, listing queries (such as contacts or receipts) are HTTP `POST` requests sending a JSON body
|
|
207
|
+
containing pagination (`limit`, `offset`), search parameters (`searchTerm`), and filters (`types`, `segments`,
|
|
208
|
+
`teamId`). The SDK handles this seamlessly:
|
|
209
|
+
|
|
210
|
+
```python
|
|
211
|
+
results = client.members.list(
|
|
212
|
+
organization_id=org_id,
|
|
213
|
+
mandate_id=mandate_id,
|
|
214
|
+
limit=50,
|
|
215
|
+
offset=0,
|
|
216
|
+
search_term="Tennis",
|
|
217
|
+
return_count=True,
|
|
218
|
+
)
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### 4. Monetary Amounts in Integer Cents
|
|
222
|
+
|
|
223
|
+
All currency amounts in invoices and receipts (e.g. `unit_amount`, `discount`, `total_gross_amount`) are represented as
|
|
224
|
+
**integer cents** (e.g., `1250` represents `12.50 EUR`) in accordance with Campai's schema.
|
|
225
|
+
|
|
226
|
+
### 5. Partial Contact Updates vs. Clearing Nulls
|
|
227
|
+
|
|
228
|
+
- **Typed Models (Sparse Updates)**: Using `ContactUpdateRequest` serializes only non-`None` attributes, ensuring
|
|
229
|
+
unmentioned fields are not overwritten or wiped:
|
|
230
|
+
```python
|
|
231
|
+
client.members.update(
|
|
232
|
+
org_id,
|
|
233
|
+
mandate_id,
|
|
234
|
+
contact_id,
|
|
235
|
+
ContactUpdateRequest(contact_type="member"),
|
|
236
|
+
)
|
|
237
|
+
```
|
|
238
|
+
- **Raw Dictionary Escape Hatch**: To explicitly set a field to `null` (e.g. clearing an address), pass a dictionary
|
|
239
|
+
payload:
|
|
240
|
+
```python
|
|
241
|
+
client.members.update(
|
|
242
|
+
org_id,
|
|
243
|
+
mandate_id,
|
|
244
|
+
contact_id,
|
|
245
|
+
{"address": None},
|
|
246
|
+
)
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### 6. Webhooks Scope & Limitations
|
|
250
|
+
|
|
251
|
+
- Campai's OpenAPI specification defines endpoints for **triggering** existing webhooks
|
|
252
|
+
(`POST /{organizationId}/oldContacts/{contactId}/triggerWebhooks` and `oldMembers`):
|
|
253
|
+
```python
|
|
254
|
+
from campai_api.models import WebhookTriggerEvent
|
|
255
|
+
|
|
256
|
+
client.webhooks.trigger_contact_webhooks(
|
|
257
|
+
organization_id=org_id,
|
|
258
|
+
contact_id=contact_id,
|
|
259
|
+
event=WebhookTriggerEvent.UPDATED, # or "updated", "created", "deleted"
|
|
260
|
+
)
|
|
261
|
+
```
|
|
262
|
+
- **Note**: Webhook subscription management (creating, listing, or deleting webhook subscription endpoints) is **not**
|
|
263
|
+
specified in Campai's OpenAPI specification and is therefore outside the scope of this SDK.
|
|
264
|
+
|
|
265
|
+
### 7. Session Lifecycle & Ownership
|
|
266
|
+
|
|
267
|
+
- When `CampaiClient` / `AsyncCampaiClient` creates its own transport session, exiting context managers or calling
|
|
268
|
+
`close()` / `aclose()` cleanly closes all HTTP connection pools.
|
|
269
|
+
- When an external `httpx.Client`, `httpx.AsyncClient`, or `SyncTransport` / `AsyncTransport` is injected, the SDK does
|
|
270
|
+
**not** close the injected instance on `close()`, giving full lifecycle control to the caller.
|
|
271
|
+
- Calling any request on a closed client raises a typed `CampaiError`.
|
|
272
|
+
|
|
273
|
+
---
|
|
274
|
+
|
|
275
|
+
## Configuration Options
|
|
276
|
+
|
|
277
|
+
Both `CampaiClient` and `AsyncCampaiClient` accept optional configuration parameters:
|
|
278
|
+
|
|
279
|
+
```python
|
|
280
|
+
from campai_api import CampaiClient
|
|
281
|
+
|
|
282
|
+
client = CampaiClient(
|
|
283
|
+
api_key="your_api_key",
|
|
284
|
+
base_url="https://cloud.campai.com/api", # Default production endpoint
|
|
285
|
+
timeout=60.0, # Request timeout in seconds
|
|
286
|
+
headers={"X-App-Name": "BillingWorker"}, # Additional default headers
|
|
287
|
+
# client=httpx.Client(...), # Optional custom httpx.Client injection
|
|
288
|
+
)
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
---
|
|
292
|
+
|
|
293
|
+
## Error Handling
|
|
294
|
+
|
|
295
|
+
All SDK exceptions inherit from `CampaiError`:
|
|
296
|
+
|
|
297
|
+
```python
|
|
298
|
+
from campai_api import CampaiClient
|
|
299
|
+
from campai_api.exceptions import (
|
|
300
|
+
AuthenticationError,
|
|
301
|
+
CampaiAPIError,
|
|
302
|
+
CampaiNetworkError,
|
|
303
|
+
NotFoundError,
|
|
304
|
+
RateLimitError,
|
|
305
|
+
)
|
|
306
|
+
|
|
307
|
+
with CampaiClient(api_key="invalid_key") as client:
|
|
308
|
+
try:
|
|
309
|
+
info = client.auth.get_info()
|
|
310
|
+
except AuthenticationError as exc:
|
|
311
|
+
print(f"Invalid API Key (HTTP 401): {exc.message}")
|
|
312
|
+
except NotFoundError as exc:
|
|
313
|
+
print(f"Resource not found (HTTP 404): {exc.message}")
|
|
314
|
+
except RateLimitError as exc:
|
|
315
|
+
print(f"Rate limited (HTTP 429): retry after {exc.retry_after} seconds")
|
|
316
|
+
except CampaiAPIError as exc:
|
|
317
|
+
print(f"API Error {exc.status_code}: {exc.message}")
|
|
318
|
+
except CampaiNetworkError as exc:
|
|
319
|
+
print(f"Network / connection error: {exc}")
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
---
|
|
323
|
+
|
|
324
|
+
## Development & Testing
|
|
325
|
+
|
|
326
|
+
```bash
|
|
327
|
+
# Run test suite
|
|
328
|
+
uv run --frozen pytest
|
|
329
|
+
|
|
330
|
+
# Run strict type checking with ty
|
|
331
|
+
uv run --frozen ty check
|
|
332
|
+
|
|
333
|
+
# Run linter and formatter checks with ruff
|
|
334
|
+
uv run --frozen ruff check .
|
|
335
|
+
uv run --frozen ruff format --check .
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
---
|
|
339
|
+
|
|
340
|
+
## License
|
|
341
|
+
|
|
342
|
+
Created from Campai's [OpenAPI](https://cloud.campai.com/api/openapi.json) specification with the aid of
|
|
343
|
+
[Junie](https://junie.jetbrains.com).
|
|
344
|
+
Licensed under [EUPL-1.2](LICENSE.txt).
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
SPDX-FileCopyrightText: 2026 Stephan Lukasczyk <stephan@dante.de>
|
|
3
|
+
|
|
4
|
+
SPDX-License-Identifier: EUPL-1.2
|
|
5
|
+
-->
|
|
6
|
+
|
|
7
|
+
# campai-api
|
|
8
|
+
|
|
9
|
+
Idiomatic, lightweight, and type-safe Python SDK for the [Campai REST API](https://cloud.campai.com/api).
|
|
10
|
+
|
|
11
|
+
[](https://www.python.org/downloads/)
|
|
12
|
+
[](https://github.com/astral-sh/ty)
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## Features
|
|
17
|
+
|
|
18
|
+
- **Minimal Dependencies**: Strictly limited to a single runtime dependency (`httpx>=0.27.0`).
|
|
19
|
+
- **Dual Native Clients**: Synchronous (`CampaiClient`) and asynchronous (`AsyncCampaiClient`) with native context
|
|
20
|
+
managers (`with` / `async with`) and explicit lifecycle methods (`close()` / `aclose()`).
|
|
21
|
+
- **Domain-Scoped Namespaces**: Logical grouping of endpoints under `client.auth`, `client.members`, `client.finance`,
|
|
22
|
+
and `client.webhooks`.
|
|
23
|
+
- **Pure Slotted Dataclasses**: Response and request structures built with standard library
|
|
24
|
+
`@dataclass(slots=True, kw_only=True)` for lightweight performance and full autocompletion.
|
|
25
|
+
- **Accurate OpenAPI Contracts**: Built against Campai's live OpenAPI specification (`https://cloud.campai.com/api`),
|
|
26
|
+
preserving exact payload constraints, integer cent amounts, and path structures.
|
|
27
|
+
- **Typed Error Hierarchy**: Clear exception mapping for HTTP status codes (e.g., `AuthenticationError` for 401,
|
|
28
|
+
`NotFoundError` for 404, `RateLimitError` for 429).
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
# Using uv (recommended)
|
|
36
|
+
uv add campai-api
|
|
37
|
+
|
|
38
|
+
# Using pip
|
|
39
|
+
pip install campai-api
|
|
40
|
+
|
|
41
|
+
# Install from source / Git repository
|
|
42
|
+
pip install git+https://codeberg.org/stephanlukasczyk/campai-api.git
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Supported Endpoints & Coverage
|
|
48
|
+
|
|
49
|
+
Campai's OpenAPI specification defines ~664 API paths. This SDK provides complete, high-level typed bindings for the
|
|
50
|
+
core business resources:
|
|
51
|
+
|
|
52
|
+
| Domain Namespace | Endpoints & Routes | Supported Capabilities |
|
|
53
|
+
|:------------------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------------------------------------------------------------------------------------------|
|
|
54
|
+
| `client.auth` | `GET /apiKey/info`<br>`GET /organizations/{orgId}`<br>`POST /organizations/{orgId}/mandates/list`<br>`GET /organizations/{orgId}/mandates/{mandateId}` | API key capability lookup, organization details, mandate listing, and retrieval. |
|
|
55
|
+
| `client.members` | `POST /{orgId}/{mandateId}/crm/contacts/list`<br>`POST /{orgId}/{mandateId}/crm/contacts`<br>`GET /{orgId}/{mandateId}/crm/contacts/{id}`<br>`POST /{orgId}/{mandateId}/crm/contacts/{id}`<br>`DELETE /{orgId}/{mandateId}/crm/contacts/{id}` | CRM contact & member search, pagination, full creation, sparse/partial updates, and deletion. |
|
|
56
|
+
| `client.finance` | `GET /{orgId}/finance/settings`<br>`POST /{orgId}/finance/settings`<br>`POST /{orgId}/{mandateId}/finance/receipts/list`<br>`POST /{orgId}/{mandateId}/receipts/invoice`<br>`POST /{orgId}/{mandateId}/receipts/invoice/{id}` | Financial settings, receipt queries/pagination, and invoice creation & updates (amounts in cents). |
|
|
57
|
+
| `client.webhooks` | `POST /{orgId}/oldContacts/{id}/triggerWebhooks`<br>`POST /{orgId}/oldMembers/{id}/triggerWebhooks` | Triggering webhook dispatches (`created`, `updated`, `deleted`) for contacts and members. |
|
|
58
|
+
|
|
59
|
+
> **Note**: For specialised or legacy endpoints outside these core modules, the underlying transport engine
|
|
60
|
+
> (`client.transport.request(...)`) can be used directly with full authentication and error mapping.
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## Quick Start
|
|
65
|
+
|
|
66
|
+
### Synchronous Client (`CampaiClient`)
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
from campai_api import (
|
|
70
|
+
Address,
|
|
71
|
+
CampaiClient,
|
|
72
|
+
ContactCreateRequest,
|
|
73
|
+
InvoiceCreateRequest,
|
|
74
|
+
InvoicePositionRequest,
|
|
75
|
+
PersonCreateInput,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
with CampaiClient(api_key="your_campai_api_key") as client:
|
|
79
|
+
# 1. Retrieve API key and organisation details
|
|
80
|
+
info = client.auth.get_info()
|
|
81
|
+
org_id = info.organization
|
|
82
|
+
print(f"Authenticated with organisation: {org_id}")
|
|
83
|
+
|
|
84
|
+
# 2. Retrieve active mandates
|
|
85
|
+
mandates_resp = client.auth.list_mandates(org_id)
|
|
86
|
+
mandate_id = mandates_resp.mandates[0].id
|
|
87
|
+
|
|
88
|
+
# 3. List CRM contacts / members
|
|
89
|
+
contacts_resp = client.members.list(
|
|
90
|
+
organization_id=org_id,
|
|
91
|
+
mandate_id=mandate_id,
|
|
92
|
+
limit=20,
|
|
93
|
+
search_term="Mustermann",
|
|
94
|
+
)
|
|
95
|
+
for contact in contacts_resp.contacts:
|
|
96
|
+
print(f"Contact: {contact.name} ({contact.id})")
|
|
97
|
+
|
|
98
|
+
# 4. Create a new contact
|
|
99
|
+
new_contact = client.members.create(
|
|
100
|
+
organization_id=org_id,
|
|
101
|
+
mandate_id=mandate_id,
|
|
102
|
+
contact=ContactCreateRequest(
|
|
103
|
+
type="person",
|
|
104
|
+
person=PersonCreateInput(
|
|
105
|
+
title="Dr.",
|
|
106
|
+
first_name="Erika",
|
|
107
|
+
last_name="Mustermann",
|
|
108
|
+
gender=None,
|
|
109
|
+
birthdate=None,
|
|
110
|
+
),
|
|
111
|
+
),
|
|
112
|
+
)
|
|
113
|
+
print(f"Created contact ID: {new_contact.id}")
|
|
114
|
+
|
|
115
|
+
# 5. Create an invoice in the finance module (amounts in cents)
|
|
116
|
+
invoice_result = client.finance.create_invoice(
|
|
117
|
+
organization_id=org_id,
|
|
118
|
+
mandate_id=mandate_id,
|
|
119
|
+
invoice=InvoiceCreateRequest(
|
|
120
|
+
draft=False,
|
|
121
|
+
address=Address(
|
|
122
|
+
address_line="Musterstr. 1",
|
|
123
|
+
zip="10115",
|
|
124
|
+
city="Berlin",
|
|
125
|
+
country="DE",
|
|
126
|
+
),
|
|
127
|
+
delivery_date_type="delivery",
|
|
128
|
+
title="Annual Membership Fee 2026",
|
|
129
|
+
intro="Thank you for being a valued member.",
|
|
130
|
+
account=8400,
|
|
131
|
+
receipt_date="2026-01-01",
|
|
132
|
+
due_date="2026-01-31",
|
|
133
|
+
positions=[
|
|
134
|
+
InvoicePositionRequest(
|
|
135
|
+
description="Annual Membership Fee",
|
|
136
|
+
unit_amount=12000, # 120.00 EUR in integer cents
|
|
137
|
+
account=8400,
|
|
138
|
+
quantity=1.0,
|
|
139
|
+
)
|
|
140
|
+
],
|
|
141
|
+
),
|
|
142
|
+
)
|
|
143
|
+
print(f"Created invoice receipt ID: {invoice_result.id}")
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Asynchronous Client (`AsyncCampaiClient`)
|
|
147
|
+
|
|
148
|
+
```python
|
|
149
|
+
import asyncio
|
|
150
|
+
from campai_api import AsyncCampaiClient
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
async def main() -> None:
|
|
154
|
+
async with AsyncCampaiClient(api_key="your_campai_api_key") as client:
|
|
155
|
+
# Retrieve organisation and mandates
|
|
156
|
+
info = await client.auth.get_info()
|
|
157
|
+
mandates_resp = await client.auth.list_mandates(info.organization)
|
|
158
|
+
mandate_id = mandates_resp.mandates[0].id
|
|
159
|
+
|
|
160
|
+
# Async list contacts
|
|
161
|
+
response = await client.members.list(
|
|
162
|
+
organization_id=info.organization,
|
|
163
|
+
mandate_id=mandate_id,
|
|
164
|
+
limit=50,
|
|
165
|
+
)
|
|
166
|
+
print(f"Found {response.count} contacts.")
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
asyncio.run(main())
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
---
|
|
173
|
+
|
|
174
|
+
## Architecture & Important API Concepts
|
|
175
|
+
|
|
176
|
+
### 1. Default Server URL & Authentication
|
|
177
|
+
|
|
178
|
+
- **Default Base URL**: `https://cloud.campai.com/api` (the `/api` path prefix is automatically preserved and validated
|
|
179
|
+
against path-traversal attacks).
|
|
180
|
+
- **Authentication**: All API requests inject the `X-API-Key` header with your token.
|
|
181
|
+
|
|
182
|
+
### 2. Organization and Mandate Scoping
|
|
183
|
+
|
|
184
|
+
Campai operates on a multi-tenant structure where CRM contacts, receipts, and invoices are partitioned under both an
|
|
185
|
+
**organisation ID** and a **mandate ID**:
|
|
186
|
+
|
|
187
|
+
- Obtain your organisation ID via `client.auth.get_info().organization`.
|
|
188
|
+
- List available mandates via `client.auth.list_mandates(org_id)` or fetch a specific mandate with
|
|
189
|
+
`client.auth.get_mandate(org_id, mandate_id)`.
|
|
190
|
+
- Pass `organization_id` and `mandate_id` as path arguments to scoped operations in `client.members` and
|
|
191
|
+
`client.finance`.
|
|
192
|
+
|
|
193
|
+
### 3. List Operations are POST Endpoints
|
|
194
|
+
|
|
195
|
+
In the Campai REST API, listing queries (such as contacts or receipts) are HTTP `POST` requests sending a JSON body
|
|
196
|
+
containing pagination (`limit`, `offset`), search parameters (`searchTerm`), and filters (`types`, `segments`,
|
|
197
|
+
`teamId`). The SDK handles this seamlessly:
|
|
198
|
+
|
|
199
|
+
```python
|
|
200
|
+
results = client.members.list(
|
|
201
|
+
organization_id=org_id,
|
|
202
|
+
mandate_id=mandate_id,
|
|
203
|
+
limit=50,
|
|
204
|
+
offset=0,
|
|
205
|
+
search_term="Tennis",
|
|
206
|
+
return_count=True,
|
|
207
|
+
)
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### 4. Monetary Amounts in Integer Cents
|
|
211
|
+
|
|
212
|
+
All currency amounts in invoices and receipts (e.g. `unit_amount`, `discount`, `total_gross_amount`) are represented as
|
|
213
|
+
**integer cents** (e.g., `1250` represents `12.50 EUR`) in accordance with Campai's schema.
|
|
214
|
+
|
|
215
|
+
### 5. Partial Contact Updates vs. Clearing Nulls
|
|
216
|
+
|
|
217
|
+
- **Typed Models (Sparse Updates)**: Using `ContactUpdateRequest` serializes only non-`None` attributes, ensuring
|
|
218
|
+
unmentioned fields are not overwritten or wiped:
|
|
219
|
+
```python
|
|
220
|
+
client.members.update(
|
|
221
|
+
org_id,
|
|
222
|
+
mandate_id,
|
|
223
|
+
contact_id,
|
|
224
|
+
ContactUpdateRequest(contact_type="member"),
|
|
225
|
+
)
|
|
226
|
+
```
|
|
227
|
+
- **Raw Dictionary Escape Hatch**: To explicitly set a field to `null` (e.g. clearing an address), pass a dictionary
|
|
228
|
+
payload:
|
|
229
|
+
```python
|
|
230
|
+
client.members.update(
|
|
231
|
+
org_id,
|
|
232
|
+
mandate_id,
|
|
233
|
+
contact_id,
|
|
234
|
+
{"address": None},
|
|
235
|
+
)
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
### 6. Webhooks Scope & Limitations
|
|
239
|
+
|
|
240
|
+
- Campai's OpenAPI specification defines endpoints for **triggering** existing webhooks
|
|
241
|
+
(`POST /{organizationId}/oldContacts/{contactId}/triggerWebhooks` and `oldMembers`):
|
|
242
|
+
```python
|
|
243
|
+
from campai_api.models import WebhookTriggerEvent
|
|
244
|
+
|
|
245
|
+
client.webhooks.trigger_contact_webhooks(
|
|
246
|
+
organization_id=org_id,
|
|
247
|
+
contact_id=contact_id,
|
|
248
|
+
event=WebhookTriggerEvent.UPDATED, # or "updated", "created", "deleted"
|
|
249
|
+
)
|
|
250
|
+
```
|
|
251
|
+
- **Note**: Webhook subscription management (creating, listing, or deleting webhook subscription endpoints) is **not**
|
|
252
|
+
specified in Campai's OpenAPI specification and is therefore outside the scope of this SDK.
|
|
253
|
+
|
|
254
|
+
### 7. Session Lifecycle & Ownership
|
|
255
|
+
|
|
256
|
+
- When `CampaiClient` / `AsyncCampaiClient` creates its own transport session, exiting context managers or calling
|
|
257
|
+
`close()` / `aclose()` cleanly closes all HTTP connection pools.
|
|
258
|
+
- When an external `httpx.Client`, `httpx.AsyncClient`, or `SyncTransport` / `AsyncTransport` is injected, the SDK does
|
|
259
|
+
**not** close the injected instance on `close()`, giving full lifecycle control to the caller.
|
|
260
|
+
- Calling any request on a closed client raises a typed `CampaiError`.
|
|
261
|
+
|
|
262
|
+
---
|
|
263
|
+
|
|
264
|
+
## Configuration Options
|
|
265
|
+
|
|
266
|
+
Both `CampaiClient` and `AsyncCampaiClient` accept optional configuration parameters:
|
|
267
|
+
|
|
268
|
+
```python
|
|
269
|
+
from campai_api import CampaiClient
|
|
270
|
+
|
|
271
|
+
client = CampaiClient(
|
|
272
|
+
api_key="your_api_key",
|
|
273
|
+
base_url="https://cloud.campai.com/api", # Default production endpoint
|
|
274
|
+
timeout=60.0, # Request timeout in seconds
|
|
275
|
+
headers={"X-App-Name": "BillingWorker"}, # Additional default headers
|
|
276
|
+
# client=httpx.Client(...), # Optional custom httpx.Client injection
|
|
277
|
+
)
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
---
|
|
281
|
+
|
|
282
|
+
## Error Handling
|
|
283
|
+
|
|
284
|
+
All SDK exceptions inherit from `CampaiError`:
|
|
285
|
+
|
|
286
|
+
```python
|
|
287
|
+
from campai_api import CampaiClient
|
|
288
|
+
from campai_api.exceptions import (
|
|
289
|
+
AuthenticationError,
|
|
290
|
+
CampaiAPIError,
|
|
291
|
+
CampaiNetworkError,
|
|
292
|
+
NotFoundError,
|
|
293
|
+
RateLimitError,
|
|
294
|
+
)
|
|
295
|
+
|
|
296
|
+
with CampaiClient(api_key="invalid_key") as client:
|
|
297
|
+
try:
|
|
298
|
+
info = client.auth.get_info()
|
|
299
|
+
except AuthenticationError as exc:
|
|
300
|
+
print(f"Invalid API Key (HTTP 401): {exc.message}")
|
|
301
|
+
except NotFoundError as exc:
|
|
302
|
+
print(f"Resource not found (HTTP 404): {exc.message}")
|
|
303
|
+
except RateLimitError as exc:
|
|
304
|
+
print(f"Rate limited (HTTP 429): retry after {exc.retry_after} seconds")
|
|
305
|
+
except CampaiAPIError as exc:
|
|
306
|
+
print(f"API Error {exc.status_code}: {exc.message}")
|
|
307
|
+
except CampaiNetworkError as exc:
|
|
308
|
+
print(f"Network / connection error: {exc}")
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
---
|
|
312
|
+
|
|
313
|
+
## Development & Testing
|
|
314
|
+
|
|
315
|
+
```bash
|
|
316
|
+
# Run test suite
|
|
317
|
+
uv run --frozen pytest
|
|
318
|
+
|
|
319
|
+
# Run strict type checking with ty
|
|
320
|
+
uv run --frozen ty check
|
|
321
|
+
|
|
322
|
+
# Run linter and formatter checks with ruff
|
|
323
|
+
uv run --frozen ruff check .
|
|
324
|
+
uv run --frozen ruff format --check .
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
---
|
|
328
|
+
|
|
329
|
+
## License
|
|
330
|
+
|
|
331
|
+
Created from Campai's [OpenAPI](https://cloud.campai.com/api/openapi.json) specification with the aid of
|
|
332
|
+
[Junie](https://junie.jetbrains.com).
|
|
333
|
+
Licensed under [EUPL-1.2](LICENSE.txt).
|