Metadata-Version: 2.5
Name: pinbridge-sdk
Version: 1.12.0
Summary: Official Python SDK for the PinBridge API
Project-URL: Homepage, https://pinbridge.io
Project-URL: Documentation, https://www.pinbridge.io/docs/python-sdk/
Project-URL: Repository, https://github.com/pinbridge/python-sdk
Project-URL: Issues, https://github.com/pinbridge/python-sdk/issues
Author: PinBridge
License: MIT License
        
        Copyright (c) 2026 pinbridge
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: api,pinbridge,pinterest,sdk
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.10
Requires-Dist: httpx<1,>=0.27.0
Requires-Dist: pydantic<3,>=2.6.0
Provides-Extra: dev
Requires-Dist: black<25,>=24.0.0; extra == 'dev'
Requires-Dist: coverage[toml]<8,>=7.0.0; extra == 'dev'
Requires-Dist: pytest-asyncio<1,>=0.23.0; extra == 'dev'
Requires-Dist: pytest-cov<6,>=5.0.0; extra == 'dev'
Requires-Dist: pytest<9,>=8.0.0; extra == 'dev'
Requires-Dist: ruff<1,>=0.5.0; extra == 'dev'
Requires-Dist: tomli<3,>=2.0.0; (python_version < '3.11') and extra == 'dev'
Provides-Extra: docs
Requires-Dist: mkdocs-material<10,>=9.5.0; extra == 'docs'
Requires-Dist: mkdocs<2,>=1.6.0; extra == 'docs'
Requires-Dist: mkdocstrings[python]<2,>=1.0; extra == 'docs'
Description-Content-Type: text/markdown

# PinBridge Python SDK

Official Python SDK for the PinBridge API, including multipart image and video asset upload support for local publishing workflows.

## Documentation

- Python SDK guide: https://www.pinbridge.io/docs/python-sdk/
- API reference: https://www.pinbridge.io/docs/api/
- Runnable examples: https://github.com/pinbridge/python-examples

The `docs/` folder holds the MkDocs source for the SDK reference. `docs/development.md`
explains how to build it locally.

## Installation

```bash
pip install pinbridge-sdk
```

For local development from source:

```bash
pip install -e .[dev]
```

## Requirements

- Python `>=3.10`
- PinBridge API URL (default: `https://api.pinbridge.io`)
- Authentication via API key and/or bearer token

## Client Initialization

```python
from pinbridge_sdk import PinbridgeClient

client = PinbridgeClient(
    base_url="https://api.pinbridge.io",  # optional
    api_key="pb_live_...",                # optional
    bearer_token=None,                     # optional
    timeout=30.0,                          # optional
    headers={"x-request-source": "my-app"},
)
```

Use as a context manager to close HTTP resources automatically:

```python
with PinbridgeClient(api_key="pb_live_...") as client:
    print(client.system.health().status)
```

## Authentication Patterns

### 1. API key auth

```python
from pinbridge_sdk import PinbridgeClient

with PinbridgeClient(api_key="pb_live_...") as client:
    keys = client.api_keys.list()
```

### 2. Login to bearer token

```python
from pinbridge_sdk import PinbridgeClient
from pinbridge_sdk.models import LoginRequest

with PinbridgeClient() as client:
    auth = client.auth.login(LoginRequest(email="you@example.com", password="super-secret"))
    client.set_bearer_token(auth.access_token)
    print(client.auth.me().workspace.name)
```

### 3. Switching auth at runtime

```python
client.set_api_key("pb_live_new")
client.set_bearer_token("new-jwt")
client.clear_auth()  # removes both
```

## Async Client

```python
from pinbridge_sdk import AsyncPinbridgeClient

async def run() -> None:
    async with AsyncPinbridgeClient(api_key="pb_live_...") as client:
        pricing = await client.billing.pricing()
        print(pricing.source)
```

## Resource Guide

All sync resources are available on `PinbridgeClient`; async equivalents have identical names on `AsyncPinbridgeClient`.

### System (`client.system`)

- `root()`
- `health()`
- `readiness()`
- `stripe_webhook(body, stripe_signature=...)`

```python
health = client.system.health()
print(health.status, health.checks)

ready = client.system.readiness()
print(ready.status, ready.database)
```

### Auth (`client.auth`)

- `register(RegisterRequest | dict)`
- `login(LoginRequest | dict)`
- `forgot_password(ForgotPasswordRequest | dict)`
- `reset_password(ResetPasswordRequest | dict)`
- `change_password(ChangePasswordRequest | dict)`
- `request_email_verification()`
- `verify_email(token=...)`
- `me()`
- `get_profile()`
- `update_profile(ProfileUpdateRequest | dict)`

### API Keys (`client.api_keys`)

- `create(APIKeyCreate | dict)` — optional `scopes` (`read` / `write` / `destructive`) and `pinterest_account_ids` allow-list
- `list()` — responses carry `scopes`, `pinterest_account_ids`, `source` (`manual` / `oauth`), `last_used_at`
- `update(key_id, APIKeyUpdate | dict)` — partial: `name`, `scopes`, `pinterest_account_ids`
- `revoke(key_id)`

### Pinterest (`client.pinterest`)

- `start_oauth()`
- `oauth_callback(code=..., state=..., follow_redirects=False)`
- `list_accounts()`
- `revoke_account(account_id)`
- `list_boards(account_id)`
- `check_board_access(board_id, account_id=..., fresh=False)` — can this account publish to this board, and why not
- `account_analytics(account_id, start_date=None, end_date=None, metrics=None, source=None, include_daily=None)` — impressions, saves, clicks per day (up to 366 days from stored history, 90 live); `include_daily=False` returns only the totals
- `list_related_terms(account_id, terms, exact_match=False)`
- `create_board(BoardCreateRequest | dict)` — `SECRET` needs `boards:write_secret` (accounts connected before API 1.37 must reconnect)
- `update_board(board_id, BoardUpdateRequest | dict)`
- `delete_board(board_id, account_id=...)` — deletes the board on Pinterest with every pin on it (irreversible)

```python
from pinbridge_sdk.models import BoardCreateRequest

accounts = client.pinterest.list_accounts()
boards = client.pinterest.list_boards(accounts[0].id)
related = client.pinterest.list_related_terms(
    accounts[0].id,
    ["workout", "yoga"],
    exact_match=True,
)
created = client.pinterest.create_board(
    BoardCreateRequest(account_id=accounts[0].id, name="SDK Board")
)
print(related.related_terms_list[0].related_terms)
```

### Projects (`client.projects`)

- `list()`
- `create_sandbox(CreateSandboxProjectRequest | dict | None = None)`
- `reset_sandbox()`
- `switch(SwitchProjectRequest | dict)`

```python
projects = client.projects.list()
sandbox = next((p for p in projects.projects if p.environment.value == "sandbox"), None)
if sandbox is None:
    sandbox = next(
        p for p in client.projects.create_sandbox().projects if p.environment.value == "sandbox"
    )
client.projects.reset_sandbox()
switched = client.projects.switch({"project_id": str(sandbox.id)})
client.set_bearer_token(switched.access_token)
```

### Pins and Jobs (`client.pins`, `client.jobs`)

- `client.assets.upload_image(file, filename=..., content_type=...)`
- `client.assets.upload_video(file, filename=..., content_type=...)`
- `client.assets.list(workspace_id=None, sort="created_at_desc", limit=50, offset=0, q=None, asset_type=None, in_use=None, since=None, until=None)` — `total` counts every match; `q` searches file names, `in_use` keeps assets used (or not) by a pin or by a scheduled pin that has not run yet
- `client.assets.get(asset_id)`
- `client.assets.get_content(asset_id)`
- `client.assets.delete(asset_id, confirm=False)` — refuses (`requires_confirmation=True`) while a pin or a pending scheduled pin uses the asset
- `client.assets.bulk_delete([asset_id, ...], confirm=False)`
- `client.pins.create(PinCreate | dict)`
- `client.pins.validate(PinCreate | dict)` — dry run: every check the API runs, nothing published
- `client.pins.create_batch([PinCreate | dict, ...])` — up to 100 pins, per-item outcome
- `client.pins.update(pin_id, PinUpdate | dict)` — edit title / description / link / alt text / board of a pin that has not published; a published pin raises `APIError` with `code == "pin_already_published"`
- `client.pins.analytics(pin_id, start_date=None, end_date=None, metrics=None, source=None, include_daily=None)`
- `client.pins.import_json(list[PinImportCreate | PinCreate | dict])`
- `client.pins.import_csv(file, filename=..., content_type=...)`
- `client.pins.get_import(job_id)`
- `client.pins.list_imports(limit=50, offset=0, status=None, source_type=None)`
- `client.pins.get(pin_id)`
- `client.pins.list(limit=50, offset=0, account_id=None, board_id=None, status=None, error_code=None, since=None, until=None, q=None, sort=None)` — `q` searches title, description and link; `sort` is one of `PinSort` (`created_at_desc` by default, `published_at_*`, `title_*`, `status_*`)
- `client.pins.list_page(...)` — same filters, returns a `Page` with `items`, `total` (from `X-Total-Count`) and `has_more`
- `client.pins.delete(pin_id, delete_from_pinterest=False)` — returns `None`, or a `PinDeleteResponse` when the Pinterest-side delete was requested
- `client.pins.retry(pin_id, PinRetryRequest | dict | None)`
- `client.pins.bulk_delete([pin_id, ...])`
- `client.pins.bulk_retry([pin_id, ...])`
- `client.jobs.get(job_id)`

```python
from pinbridge_sdk.models import PinCreate, PinUpdate

draft = PinCreate(account_id=account.id, board_id=board.id, title="Autumn recipes",
                  image_url="https://example.com/soup.jpg", idempotency_key="soup-2026-09")
check = client.pins.validate(draft)            # nothing published
if check.valid:
    pin = client.pins.create(draft)
    client.pins.update(pin.id, PinUpdate(title="Autumn soup recipes"))
    stats = client.pins.analytics(pin.id, metrics=["IMPRESSION", "SAVE"])
else:
    print([(c.name, c.code, c.remediation) for c in check.checks if c.status.value == "failed"])
```

Deletes on an asset used by pins, or by scheduled pins that have not run yet (API 1.38.1+), return
`requires_confirmation=True`; pass `confirm=True` to force the deletion. An unpublished pin or
schedule that used the asset then fails to publish. Bulk actions (`bulk_delete`, `bulk_retry`) return a `BulkOperationResponse`
with per-item `results` (`succeeded` / `skipped` / `failed`).

```python
from pinbridge_sdk.models import PinCreate, PinImportCreate

asset = client.assets.upload_image(
    "./pin-image.png",
    content_type="image/png",
)

pin = client.pins.create(
    PinCreate(
        account_id="...",
        board_id="...",
        title="Hello",
        description="From SDK",
        alt_text="Descriptive alt text for accessibility",
        dominant_color="#E88A2D",
        asset_id=asset.id,
        idempotency_key="my-idempotency-key",
    )
)
status = client.jobs.get(pin.id)
print(status.status)
```

```python
video_asset = client.assets.upload_video(
    "./pin-video.mp4",
    content_type="video/mp4",
)

video_pin = client.pins.create(
    PinCreate(
        account_id="...",
        board_id="...",
        title="Video launch",
        asset_id=video_asset.id,
        cover_image_url="https://cdn.example.com/video-cover.jpg",
        idempotency_key="video-idempotency-key",
    )
)
print(video_pin.media_type.value, video_pin.media_url)
```

```python
import_job = client.pins.import_json(
    [
        PinImportCreate(
            account_id="...",
            board_id="...",
            title="Bulk one",
            image_url="https://example.com/bulk-1.jpg",
            idempotency_key="bulk-json-1",
            run_at="2026-03-06T10:00:00Z",  # optional: omit for immediate queueing
        ),
        {
            "account_id": "...",
            "board_id": "...",
            "title": "Bulk two",
            "image_url": "https://example.com/bulk-2.jpg",
            "idempotency_key": "bulk-json-2",
        },
    ]
)
print(import_job.status.value)
print(client.pins.get_import(import_job.id).processed_rows)
```

For scheduled publishing fields (`run_at` on schedules/import rows), send absolute ISO 8601
timestamps with an explicit timezone offset (for example `2026-03-06T10:00:00Z`).

### Schedules (`client.schedules`)

- `create(ScheduleCreate | dict)`
- `validate(ScheduleCreate | dict)` — dry run, including `run_at`
- `get(schedule_id)`
- `list(limit=50, offset=0, account_id=None, board_id=None, status=None, since=None, until=None, q=None, sort=None)` — `sort` is one of `ScheduleSort`; `run_at_asc` lists the next run first
- `list_page(...)` — same filters, returns a `Page` with `items`, `total` and `has_more`
- `update(schedule_id, ScheduleUpdate | dict)` — edit a pending schedule in place (time, board, text, media)
- `cancel(schedule_id)`
- `retry(schedule_id)`
- `delete(schedule_id)`
- `bulk_cancel([schedule_id, ...])`
- `bulk_retry([schedule_id, ...])`
- `bulk_delete([schedule_id, ...])`

Pins and schedules accept a public `image_url`, an uploaded `asset_id`, or `template_inputs` to draw the image from a template (see below). Video publishes and schedules should use uploaded assets.
Pinterest-compatible limits are enforced in SDK models: `title <= 100`, `description <= 800`,
`alt_text <= 500`, and URLs (`link_url`, `cover_image_url`) `<= 2048`.

### Templates (`client.templates`)

- `list(limit=50, offset=0, source=None, niche=None, q=None, sort=None)`: the workspace's own templates and the gallery. `source` is `workspace` or `gallery`, `niche` one slug or a list, `sort` one of `updated` (default), `pins`, `copies`, `name`
- `list_page(...)`: same filters, returns a `Page` with `items`, `total` and `has_more`
- `get(template_id, gallery=False)`: one template with its `definition`. `inputs` lists what a pin can set and `size` is the pin size it draws

A pin or schedule drawn from a template sends `template_id` and `template_inputs` (values by
input name) and leaves `image_url` and `asset_id` out. PinBridge draws the image at the
template's size, then publishes it. A schedule is drawn when it is created. `template_version`
picks a version other than the current one.

```python
from datetime import datetime, timezone

from pinbridge_sdk.models import PinCreate, ScheduleCreate

templates = client.templates.list(source="gallery", niche="food", q="recipe")
template = client.templates.get(templates[0].id)
for name, spec in template.inputs.items():
    print(name, spec.type, "required" if spec.required else "optional", spec.max_length)

inputs = {
    "title": "Lemon pasta in 15 minutes",
    "photo": {"src": "https://example.com/pasta.jpg", "focus": [0.5, 0.4]},
}
pin = client.pins.create(
    PinCreate(
        account_id=account.id,
        board_id=board.id,
        title="Lemon pasta",
        link_url="https://example.com/lemon-pasta",
        template_id=template.id,
        template_inputs=inputs,
        idempotency_key="lemon-pasta-1",
    )
)
print(pin.template_id, pin.template_version)

schedule = client.schedules.create(
    ScheduleCreate(
        account_id=account.id,
        board_id=board.id,
        title="Lemon pasta",
        run_at=datetime(2026, 12, 1, 9, 0, tzinfo=timezone.utc),
        template_id=template.id,
        template_inputs=inputs,
    )
)
```

A photo input takes an image URL, `{"src": url, "focus": [x, y], "zoom": 1 to 3, "flip": true}`,
or `{"asset_id": id}` with the same options (focus values go from 0 to 1). `client.pins.validate(...)` draws the pin and
throws it away, so you can check the inputs first. Batches (`create_batch`) and imports don't
draw from templates yet; send those pins one at a time. Drawing can take up to a minute when
the renderers are busy, so requests with `template_inputs` wait at least 90 seconds for the
answer.

### Dashboard (`client.dashboard`)

- `summary(start=None, end=None, tz=None, account_id=None)` — publishing activity for `[start, end)`:
  pin counts by status and success rate, the same figures for the previous period of equal
  length, an hourly (ranges up to 48 hours) or daily series bucketed in `tz`, published pins per
  account, the current queue, schedules and import jobs. Defaults to the last 30 days; up to 366
  days. A naive `start`/`end` is read as wall-clock time in `tz`.

```python
from datetime import datetime

week = client.dashboard.summary(start=datetime(2026, 9, 1), end=datetime(2026, 9, 8), tz="Europe/Paris")
print(week.pins.total, week.pins.success_rate, week.previous_pins.total)
for point in week.series:
    print(point.start.date(), point.created, point.published, point.failed)

page = client.pins.list_page(account_id=account.id, q="soup", sort="created_at_desc", limit=25)
print(f"{len(page.items)} of {page.total}", "more" if page.has_more else "done")
```

Search, sort, `list_page` totals, the asset filters and the dashboard need API 1.34.0 or later.

### Webhooks (`client.webhooks`)

- `create(WebhookCreate | dict)`
- `list()`
- `get(webhook_id)`
- `update(webhook_id, WebhookUpdate | dict)`
- `delete(webhook_id)`

### Billing and Rate Meter (`client.billing`, `client.rate_meter`)

- `client.billing.pricing()`
- `client.billing.checkout(CheckoutRequest | dict)`
- `client.billing.portal()`
- `client.billing.status()`
- `client.rate_meter.get(account_id)`

### Team (`client.team`)

- `preview_invitation(token)`
- `list_members()`
- `list_invitations()`
- `create_invitation(TeamInvitationCreateRequest | dict)`
- `resend_invitation(invitation_id)`
- `revoke_invitation(invitation_id)`
- `update_member(member_id, TeamMemberUpdateRequest | dict)`
- `remove_member(member_id)`
- `accept_invitation(TeamInvitationAcceptRequest | dict)`

```python
from pinbridge_sdk.models import TeamInvitationCreateRequest

invitation = client.team.create_invitation(
    TeamInvitationCreateRequest(email="teammate@example.com", role="editor")
)
members = client.team.list_members()
```

### MCP Usage (`client.mcp`)

- `quota()` — current-week MCP request usage and quota for the workspace
- `track()` — increment the counter after a tool call, returning the updated usage

### Email Preferences (`client.email`)

- `get_preferences()`
- `update_preferences(EmailPreferencesUpdateRequest | dict)`
- `unsubscribe(workspace_id=..., token=..., user_id=...)` — public token-based unsubscribe

## Typed Models

All methods return typed Pydantic models from `pinbridge_sdk.models`.

Use either model instances or plain dictionaries as method input.

```python
from pinbridge_sdk.models import WebhookCreate

created = client.webhooks.create(
    WebhookCreate(
        url="https://example.com/hook",
        secret="0123456789012345",
        events=["pin.published", "pin.failed"],
    )
)
```

## Error Handling

Raised exceptions:

- `pinbridge_sdk.AuthenticationError`
- `pinbridge_sdk.NotFoundError`
- `pinbridge_sdk.ValidationError`
- `pinbridge_sdk.RateLimitError`
- `pinbridge_sdk.APIError`

```python
from pinbridge_sdk import APIError, PinbridgeClient

try:
    with PinbridgeClient(api_key="bad") as client:
        client.auth.me()
except APIError as exc:
    print(exc.status_code, exc.message, exc.code)
```

Drawing a pin from a template can also raise:

- `pinbridge_sdk.TemplateInputsInvalidError` (422, a `ValidationError`): `problems` lists each bad input with its `code` (`required`, `too_long`, `wrong_type`, ...) and `message`
- `pinbridge_sdk.TemplatesNotAvailableError`: pin templates are not turned on for the workspace yet
- `pinbridge_sdk.RenderBusyError` (503): every renderer is busy; send the same request again after `retry_after` seconds
- `pinbridge_sdk.RenderUnavailableError` (503): pins can't be drawn right now; `RenderBusyError` is a subclass

Nothing is created when one of these is raised. Other template codes keep the class of their
status code, with `code` set: `template_not_found` and `template_version_not_found` on a pin or
schedule are a `ValidationError` (422), for example.

```python
import time

from pinbridge_sdk import RenderBusyError, TemplateInputsInvalidError

try:
    pin = client.pins.create(draft)
except TemplateInputsInvalidError as exc:
    for problem in exc.problems:
        print(problem.input, problem.code, problem.message)
except RenderBusyError as exc:
    time.sleep(exc.retry_after or 5)
    pin = client.pins.create(draft)  # same idempotency_key, so no duplicate
```

## Extending the SDK

You can register custom resources without changing core classes.

```python
from pinbridge_sdk import PinbridgeClient
from pinbridge_sdk.resources.base import SyncAPIResource

class DiagnosticsResource(SyncAPIResource):
    def ping(self):
        return self._request("GET", "/healthz").json()

with PinbridgeClient(api_key="pb_live_...") as client:
    client.register_resource("diagnostics", DiagnosticsResource)
    print(client.diagnostics.ping())
```

This keeps new API groups low-risk: add models + resource class and register/bind it.

## Testing, Formatting, Coverage

```bash
black .
ruff check .
pytest --cov=pinbridge_sdk --cov-config=.coveragerc --cov-report=term-missing --cov-report=xml
```

Coverage config file: `.coveragerc`

Detailed release runbook: `RELEASING.md`
