# Call the Automation API

Create a scoped server credential, make an authenticated request, and use the exact OpenAPI contract for push, SMS, audiences, bindings, and releases.

Source: https://appportal.io/docs/api/automation

Use the Automation API from your backend for privileged app actions. Installed-app SDKs use
their own limited credential; browser sessions and MCP OAuth profiles have different purposes.

## Hosts and exact contract

| Deployment | Base URL |
| --- | --- |
| Production | `https://automation.appportal.io` |
| Stage preview | `https://automation.stage.appportal.io` |

The host serves its contract at `/.well-known/appportal-automation-openapi.json`.
The [OpenAPI JSON bundled with these docs](https://appportal.io/docs/api/automation-openapi.json) is generated from
the same host-owned specification. Use the deployed host's contract when comparing release versions.

## Create the credential

In your app, open **App settings → Server API → Create server API credential**.
Select the environment, name, expiry, and only the permissions needed by the integration.
Copy the `aps_` credential once into your backend's secret store. A read-only audience test needs
`AudienceRead`; sending push needs `PushSend`. Optional network restrictions must match the
backend's egress address.

Never put this credential in a mobile app, browser bundle, URL, or agent configuration.

## First authenticated read

Set `APPPORTAL_SERVER_KEY` through your server's secret mechanism and `APPPORTAL_AUTOMATION_URL`
to the chosen base URL. Then run this from that server or a secure development shell:

```bash
curl --fail-with-body \
  --header "X-AppPortal-Server-Key: $APPPORTAL_SERVER_KEY" \
  "$APPPORTAL_AUTOMATION_URL/v1/automation/audiences"
```

A successful response is HTTP 200 with a JSON array. A new app can correctly return:

```json
[]
```

The credential chooses the account, app, and environment. Do not add scope coordinates to retarget it.

The source contract also provides `GET /v1/automation/audiences/page?limit=25` for bounded navigation.
It returns `items`, an opaque `nextCursor`, and `totalCount: null`. Continue with the exact cursor and
unchanged limit (1–100); a null cursor ends that traversal. Current credential authority and the
resolved data owner remain checked. The legacy array stays whole within 100 environment-visible
audiences and returns `409 pagination_required` above that bound. Check the chosen host's own
contract before using the page route; source coverage does not prove deployment.

## Preview a push without sending

Create `push-preview.json` with a real test user's opaque ID:

```json
{
  "title": "Integration test",
  "body": "A test notification for the development device.",
  "userIds": ["test-user-42"],
  "dryRun": true
}
```

Use a credential with `PushSend`. Choose a unique idempotency key for this operation:

```bash
curl --fail-with-body \
  --header "X-AppPortal-Server-Key: $APPPORTAL_SERVER_KEY" \
  --header "Content-Type: application/json" \
  --header "Idempotency-Key: docs-push-preview-001" \
  --data-binary @push-preview.json \
  "$APPPORTAL_AUTOMATION_URL/v1/automation/push"
```

The dry run resolves eligibility and reach without contacting a push provider. Zero reachable
recipients is a valid result when your test user has no eligible registration.
Follow [messaging setup](https://appportal.io/docs/sdks/messaging) before an actual test send.

## Errors, retries, and scope

| Status | Meaning | Action |
| --- | --- | --- |
| 400 | Invalid request or missing required idempotency key | Correct the request using the contract |
| 401 | Missing, expired, revoked, or unknown credential | Check the credential and deployment |
| 403 | Wrong class, missing permission, policy, or network restriction | Resolve access; do not retry with an app key |
| 404 | Resource absent or outside this credential's scope | Check the selected app/environment |
| 409 | Operation in flight or idempotency key reused for different input | Retry only the same logical operation with the same key |
| 429 / 5xx | Throttling or temporary service failure | Respect Retry-After when present; back off |

For a retry of the same idempotent operation, keep the same request and idempotency key. Use a new
key when changing a dry run into a real send. Do not blindly repeat a paid send after an ambiguous
failure. Validation messages can contain an `error` field; do not assume every response has the
same JSON error envelope.

Audience listing currently returns an array, not a cursor-paged collection. Other surfaces may use
different pagination; follow their contracts rather than adding undocumented page parameters.

## Next tasks

Use the contract for `/v1/automation/sms`, audience management,
`/v1/automation/user-bindings`, and release publication.
[Distribute a build](https://appportal.io/docs/guides/distribution) explains the separate upload and publish steps.
For agent reads, use [MCP](https://appportal.io/docs/api/mcp).
