{
  "openapi": "3.1.0",
  "info": {
    "title": "AppPortal Automation API",
    "version": "v1",
    "description": "Customer-server operations authenticated only by a confidential, app/environment-bound aps_ credential. App apc_, upload apu_, browser, and internal credentials are rejected."
  },
  "servers": [
    {
      "url": "/"
    }
  ],
  "security": [
    {
      "ServerCredential": []
    }
  ],
  "x-appportal-credential-classes": {
    "accepted": "aps_",
    "rejected": [
      "apc_",
      "apu_",
      "browser session",
      "internal service key"
    ]
  },
  "paths": {
    "/v1/automation/push": {
      "post": {
        "operationId": "sendPush",
        "summary": "Send or dry-run an app-scoped push notification",
        "x-appportal-permission": "PushSend",
        "parameters": [
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/AutomationPushRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Completed or replayed result"
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthenticated"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "409": {
            "$ref": "#/components/responses/Conflict"
          }
        }
      }
    },
    "/v1/automation/sms": {
      "post": {
        "operationId": "sendSms",
        "summary": "Send or dry-run consented, spend-gated SMS",
        "x-appportal-permission": "SmsSend",
        "parameters": [
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/AutomationSmsRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Completed or replayed result"
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthenticated"
          },
          "402": {
            "description": "Blocked by spend policy"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "409": {
            "$ref": "#/components/responses/Conflict"
          }
        }
      }
    },
    "/v1/automation/user-bindings": {
      "post": {
        "operationId": "issueUserBinding",
        "summary": "Issue a short-lived user/installation binding for an installed app",
        "x-appportal-permission": "UserBindingIssue",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateClientUserBindingRequest"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Binding issued; its raw apb_ value is returned once"
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthenticated"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "503": {
            "description": "Identity binding authority unavailable"
          }
        }
      }
    },
    "/v1/automation/user-bindings/revoke": {
      "post": {
        "operationId": "revokeUserBindings",
        "summary": "Revoke one installation binding or every binding for a user",
        "x-appportal-permission": "UserBindingIssue",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/RevokeClientUserBindingsRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Bindings revoked"
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthenticated"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "503": {
            "description": "Identity binding authority unavailable"
          }
        }
      }
    },
    "/v1/automation/audiences": {
      "get": {
        "operationId": "listAudiences",
        "summary": "List saved audiences in the credential's app environment",
        "description": "Whole environment-scoped native audience array in existing name order when at most100 rows qualify. More returns409 pagination_required; never truncated success. Existing ignored query parameters remain ignored. Use /page for continuation.",
        "x-appportal-permission": "AudienceRead",
        "responses": {
          "200": {
            "description": "Environment-scoped audience definitions",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "maxItems": 100,
                  "items": {
                    "$ref": "#/components/schemas/AudienceDefinition"
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthenticated"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "409": {
            "description": "pagination_required: use /v1/automation/audiences/page with limit/cursor.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/AutomationAudienceProblem"
                }
              }
            }
          },
          "503": {
            "description": "automation_audience_page_unavailable: source cannot be confirmed; never empty success.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/AutomationAudienceProblem"
                }
              }
            }
          }
        }
      },
      "post": {
        "operationId": "createAudience",
        "summary": "Create a saved audience in the credential's app environment",
        "x-appportal-permission": "AudienceWrite",
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string",
              "maxLength": 200
            },
            "description": "Resend the same key and input to recover the original audience after a lost response."
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/AudienceUpsertRequest"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Audience created",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AudienceDefinition"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthenticated"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "409": {
            "description": "The key was used for another request or storage owner."
          },
          "503": {
            "description": "Retry the original request with the same key."
          }
        }
      }
    },
    "/v1/automation/audiences/page": {
      "get": {
        "operationId": "listAudiencePage",
        "summary": "Read one bounded audience page in the current server credential environment",
        "x-appportal-permission": "AudienceRead",
        "description": "Same server authority as the legacy read. Strict limit defaults25/range1..100; cursor binds current credential, app/environment, selected data owner and limit. Environment is filtered in SQL before the bound. Immutable audience IDs use PostgreSQL C ascending; separate requests are not a frozen snapshot. Credential authority/network and source owner are checked before response emission. No create/write/send authority is added.",
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 25,
              "minimum": 1,
              "maximum": 100
            }
          },
          {
            "name": "cursor",
            "in": "query",
            "schema": {
              "type": "string",
              "minLength": 1,
              "maxLength": 8192
            },
            "description": "Opaque unchanged protected continuation, no whitespace/control characters. Unknown/repeated query keys are invalid."
          }
        ],
        "responses": {
          "200": {
            "description": "Current authorized audience page; unknown totalCount remains null.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AudiencePage"
                }
              }
            },
            "headers": {
              "Cache-Control": {
                "schema": {
                  "const": "private, no-store"
                }
              }
            }
          },
          "400": {
            "description": "automation_audience_page_invalid: malformed/repeated/unsupported selection or mismatched continuation.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/AutomationAudienceProblem"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthenticated"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "429": {
            "description": "Current account/key request budget exceeded; honor Retry-After."
          },
          "503": {
            "description": "automation_audience_page_unavailable: selected data owner unavailable or changed; no rows returned.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/AutomationAudienceProblem"
                }
              }
            }
          }
        }
      }
    },
    "/v1/automation/audiences/{audienceId}": {
      "parameters": [
        {
          "name": "audienceId",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "get": {
        "operationId": "getAudience",
        "summary": "Get one saved audience",
        "x-appportal-permission": "AudienceRead",
        "responses": {
          "200": {
            "description": "Audience definition",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AudienceDefinition"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthenticated"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "description": "Audience is outside the credential scope or does not exist"
          }
        }
      },
      "put": {
        "operationId": "updateAudience",
        "summary": "Replace a saved audience",
        "x-appportal-permission": "AudienceWrite",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/AudienceUpsertRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Audience updated",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AudienceDefinition"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthenticated"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "description": "Audience is outside the credential scope or does not exist"
          }
        }
      },
      "delete": {
        "operationId": "deleteAudience",
        "summary": "Delete a saved audience",
        "x-appportal-permission": "AudienceWrite",
        "responses": {
          "204": {
            "description": "Audience deleted"
          },
          "401": {
            "$ref": "#/components/responses/Unauthenticated"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "description": "Audience is outside the credential scope or does not exist"
          }
        }
      }
    },
    "/v1/automation/audiences/{audienceId}/estimate": {
      "post": {
        "operationId": "estimateAudience",
        "summary": "Estimate the current reach of a saved audience",
        "x-appportal-permission": "AudienceRead",
        "parameters": [
          {
            "name": "audienceId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Current audience estimate"
          },
          "401": {
            "$ref": "#/components/responses/Unauthenticated"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "description": "Audience is outside the credential scope or does not exist"
          }
        }
      }
    },
    "/v1/automation/releases/{artifactId}/publish": {
      "post": {
        "operationId": "publishRelease",
        "summary": "Publish an uploaded artifact to a release channel",
        "x-appportal-permission": "ReleasePublish",
        "parameters": [
          {
            "name": "artifactId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/AutomationReleasePublishRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Release published or replayed"
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthenticated"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "description": "Artifact or channel is outside the credential scope or does not exist"
          },
          "409": {
            "$ref": "#/components/responses/Conflict"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ServerCredential": {
        "type": "apiKey",
        "in": "header",
        "name": "X-AppPortal-Server-Key",
        "description": "A confidential aps_ credential. Store only in a customer-server secret store."
      }
    },
    "parameters": {
      "IdempotencyKey": {
        "name": "Idempotency-Key",
        "in": "header",
        "required": true,
        "schema": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        }
      }
    },
    "responses": {
      "BadRequest": {
        "description": "Invalid request or missing idempotency key"
      },
      "Unauthenticated": {
        "description": "Missing, invalid, revoked, or expired credential"
      },
      "Forbidden": {
        "description": "Wrong credential class, missing permission, scope violation, or network restriction"
      },
      "Conflict": {
        "description": "Idempotency key is in flight or was reused for another request"
      }
    },
    "schemas": {
      "AutomationPushRequest": {
        "type": "object",
        "required": [
          "title",
          "body"
        ],
        "properties": {
          "title": {
            "type": "string",
            "maxLength": 200
          },
          "body": {
            "type": "string",
            "maxLength": 4096
          },
          "userIds": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "userEmail": {
            "type": [
              "string",
              "null"
            ]
          },
          "tags": {
            "type": "object",
            "additionalProperties": {
              "type": "string"
            }
          },
          "platforms": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "locality": {
            "type": [
              "string",
              "null"
            ]
          },
          "deepLink": {
            "type": [
              "string",
              "null"
            ]
          },
          "data": {
            "type": "object",
            "additionalProperties": {
              "type": "string"
            }
          },
          "ttlSeconds": {
            "type": [
              "integer",
              "null"
            ],
            "minimum": 1,
            "maximum": 2419200
          },
          "silent": {
            "type": "boolean"
          },
          "priority": {
            "type": [
              "string",
              "null"
            ],
            "enum": [
              "low",
              "normal",
              "high",
              null
            ]
          },
          "dryRun": {
            "type": "boolean"
          }
        }
      },
      "AutomationSmsRequest": {
        "type": "object",
        "required": [
          "body"
        ],
        "properties": {
          "body": {
            "type": "string",
            "maxLength": 4096
          },
          "userIds": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "userEmail": {
            "type": [
              "string",
              "null"
            ]
          },
          "tags": {
            "type": "object",
            "additionalProperties": {
              "type": "string"
            }
          },
          "platforms": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "locality": {
            "type": [
              "string",
              "null"
            ]
          },
          "category": {
            "type": "string",
            "enum": [
              "Marketing",
              "Transactional",
              "Conversational"
            ]
          },
          "dryRun": {
            "type": "boolean"
          }
        }
      },
      "CreateClientUserBindingRequest": {
        "type": "object",
        "required": [
          "userId",
          "installationId"
        ],
        "properties": {
          "userId": {
            "type": "string"
          },
          "installationId": {
            "type": "string"
          },
          "operations": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "lifetimeSeconds": {
            "type": [
              "integer",
              "null"
            ],
            "minimum": 60,
            "maximum": 86400
          }
        }
      },
      "RevokeClientUserBindingsRequest": {
        "type": "object",
        "required": [
          "userId"
        ],
        "properties": {
          "userId": {
            "type": "string"
          },
          "installationId": {
            "type": [
              "string",
              "null"
            ]
          },
          "reason": {
            "type": [
              "string",
              "null"
            ]
          }
        }
      },
      "AudienceUpsertRequest": {
        "type": "object",
        "required": [
          "name"
        ],
        "properties": {
          "name": {
            "type": "string"
          },
          "userIds": {
            "type": [
              "array",
              "null"
            ],
            "items": {
              "type": "string"
            }
          },
          "platforms": {
            "type": [
              "array",
              "null"
            ],
            "items": {
              "type": "string"
            }
          },
          "tags": {
            "type": [
              "object",
              "null"
            ],
            "additionalProperties": {
              "type": "string"
            }
          },
          "locality": {
            "type": [
              "string",
              "null"
            ]
          },
          "environmentName": {
            "type": [
              "string",
              "null"
            ],
            "description": "May be omitted or must equal the aps_ credential environment."
          },
          "minAppVersion": {
            "type": [
              "string",
              "null"
            ]
          },
          "dormantDays": {
            "type": [
              "integer",
              "null"
            ],
            "minimum": 0
          }
        }
      },
      "AudienceDefinition": {
        "type": "object",
        "required": [
          "audienceId",
          "tenantId",
          "appId",
          "name",
          "userIds",
          "platforms",
          "tags",
          "locality",
          "environmentName",
          "minAppVersion",
          "dormantDays"
        ],
        "properties": {
          "audienceId": {
            "type": "string"
          },
          "tenantId": {
            "type": "string"
          },
          "appId": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "userIds": {
            "type": [
              "array",
              "null"
            ],
            "items": {
              "type": "string"
            }
          },
          "platforms": {
            "type": [
              "array",
              "null"
            ],
            "items": {
              "type": [
                "string",
                "integer"
              ]
            },
            "description": "Native AppPlatform serializer values; defined values use their native names, while unknown persisted enum numbers remain numbers."
          },
          "tags": {
            "type": [
              "object",
              "null"
            ],
            "additionalProperties": {
              "type": "string"
            }
          },
          "locality": {
            "type": [
              "string",
              "null"
            ]
          },
          "environmentName": {
            "type": [
              "string",
              "null"
            ]
          },
          "minAppVersion": {
            "type": [
              "string",
              "null"
            ]
          },
          "dormantDays": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int32"
          }
        }
      },
      "AudiencePage": {
        "type": "object",
        "required": [
          "items",
          "nextCursor",
          "totalCount"
        ],
        "properties": {
          "items": {
            "type": "array",
            "maxItems": 100,
            "items": {
              "$ref": "#/components/schemas/AudienceDefinition"
            }
          },
          "nextCursor": {
            "type": [
              "string",
              "null"
            ],
            "description": "Protected source/credential/scope/limit-bound continuation. Null when this read has no following row."
          },
          "totalCount": {
            "type": [
              "integer",
              "null"
            ],
            "format": "int64",
            "description": "Always null: no complete snapshot count is asserted."
          }
        }
      },
      "AutomationAudienceProblem": {
        "type": "object",
        "required": [
          "type",
          "title",
          "status",
          "detail",
          "instance",
          "code",
          "message",
          "error",
          "traceId"
        ],
        "properties": {
          "type": {
            "type": "string"
          },
          "title": {
            "type": "string"
          },
          "status": {
            "type": "integer",
            "minimum": 400,
            "maximum": 599
          },
          "detail": {
            "type": "string"
          },
          "instance": {
            "type": "string"
          },
          "code": {
            "type": "string"
          },
          "message": {
            "type": "string"
          },
          "error": {
            "type": "string"
          },
          "traceId": {
            "type": "string"
          }
        },
        "description": "Native ApiProblemContract from shared ServiceDefaults. instance is an opaque request URN, not a resource URL; code/message/error/detail are server-authored."
      },
      "AutomationReleasePublishRequest": {
        "type": "object",
        "required": [
          "channelId"
        ],
        "properties": {
          "channelId": {
            "type": "string"
          }
        }
      }
    }
  }
}
