{
  "openapi": "3.1.0",
  "info": {
    "title": "Percard ID Cardholder API",
    "version": "1.2.3",
    "x-last-updated": "2026-11-04",
    "description": "Server-to-server API for sending cardholders to Percard ID. Browser card preview and positioning approval use the optional approval.url returned with live submissions. API keys stay on your server. Guide: https://percardid.com/developers/api. No standalone image/PDF preview endpoint is available. Reference update only; the API route remains /v1."
  },
  "servers": [
    {
      "url": "https://ahokzlxudlwhizmjrlfv.supabase.co/functions/v1/corporate-api/v1"
    }
  ],
  "security": [
    {
      "apiKey": []
    }
  ],
  "components": {
    "securitySchemes": {
      "apiKey": {
        "type": "http",
        "scheme": "bearer",
        "description": "pcid_live_… or pcid_test_…"
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "object",
            "required": [
              "code",
              "message"
            ],
            "properties": {
              "code": {
                "type": "string"
              },
              "message": {
                "type": "string"
              },
              "details": {}
            }
          }
        },
        "required": [
          "error"
        ]
      },
      "CardholderIn": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "external_id": {
            "type": "string",
            "maxLength": 100,
            "description": "Your stable ID for this person. Retries for a saved cardholder return duplicate without updating it. Do not blindly resubmit held rows.",
            "minLength": 1
          },
          "fields": {
            "type": "object",
            "additionalProperties": true,
            "description": "Field keys from GET /forms, including the configured address field for per-cardholder shipping. Account order, shipping, pricing and billing settings are not payload fields.",
            "default": {}
          },
          "photo_url": {
            "type": "string",
            "format": "uri",
            "description": "Public https URL, max 10 MB",
            "maxLength": 2000
          },
          "photo_base64": {
            "type": "string",
            "description": "Base64 image, max 10 MB"
          },
          "form": {
            "type": "string",
            "description": "Form short name from GET /forms. Optional when the account has one form.",
            "minLength": 1,
            "maxLength": 64
          }
        },
        "description": "Send photo_url or photo_base64, not both. Photo transforms and approval flags are not accepted; use the approval screen. Test submissions validate without storing a cardholder, photo, order or approval link."
      },
      "RowResult": {
        "type": "object",
        "properties": {
          "ok": {
            "type": "boolean"
          },
          "id": {
            "type": "string",
            "description": "Saved submission ID, held_… ID for held rows, or temporary test_… ID for test mode. Temporary test IDs do not refer to saved cardholders."
          },
          "external_id": {
            "type": [
              "string",
              "null"
            ]
          },
          "status": {
            "type": "string",
            "enum": [
              "received",
              "duplicate",
              "held",
              "test_accepted"
            ],
            "description": "Submission result, distinct from status lookup. test_accepted validates only; duplicate returns an existing saved card without changing it."
          },
          "error": {
            "$ref": "#/components/schemas/Error/properties/error"
          },
          "form": {
            "type": "string"
          },
          "form_version": {
            "type": "integer"
          },
          "order_number": {
            "type": "integer",
            "description": "Present when your account is set to one order per cardholder and an order was created for this cardholder."
          },
          "held_reason": {
            "type": "string",
            "enum": [
              "daily_cap",
              "form_changed"
            ]
          },
          "warnings": {
            "type": "array",
            "items": {
              "type": "object"
            },
            "description": "Validation warnings for eligible held form_changed submissions."
          }
        },
        "required": [
          "ok",
          "external_id"
        ],
        "description": "Successful rows have id and status; failed batch rows have error. Received means accepted, not client-approved, printed or paid. Held rows are stored for release and do not create approval links."
      },
      "Status": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "external_id": {
            "type": [
              "string",
              "null"
            ]
          },
          "status": {
            "type": "string",
            "enum": [
              "received",
              "confirmed",
              "in_production",
              "printed",
              "shipped",
              "replaced"
            ]
          },
          "submitted_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          },
          "confirmed_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          },
          "printed_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          },
          "shipped_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          },
          "tracking": {
            "type": [
              "object",
              "null"
            ],
            "properties": {
              "carrier": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "number": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "url": {
                "type": [
                  "string",
                  "null"
                ]
              }
            },
            "description": "Null before shipping. Nested carrier/number/url; per-cardholder shipment takes priority over order-level tracking. Untracked mail has no number or URL."
          }
        },
        "description": "Status-only view. No names, photos, addresses or form answers, and no separate awaiting_client_approval status or approval timestamp. For the browser approval step use popup messages.",
        "required": [
          "id",
          "external_id",
          "status",
          "submitted_at",
          "confirmed_at",
          "printed_at",
          "shipped_at",
          "tracking"
        ]
      },
      "Approval": {
        "type": "object",
        "required": [
          "required",
          "url",
          "cards",
          "expires_at"
        ],
        "description": "Optional, best-effort top-level object for live accounts requiring card approval. Covers received/duplicate rows only, not held or failed rows. Missing approval does not prove approval is unnecessary. Anyone with this URL can review and approve its scoped cards; do not publish or log the token.",
        "properties": {
          "required": {
            "type": "boolean",
            "const": true
          },
          "url": {
            "type": "string",
            "format": "uri",
            "description": "No-sign-in browser approval page at https://percardid.com/approve/{token}. Open a popup on user action; no automatic embedding or approval-link email."
          },
          "cards": {
            "type": "integer",
            "minimum": 1
          },
          "expires_at": {
            "type": "string",
            "format": "date-time",
            "description": "Expires 30 days after creation. Use Saved Cards or request a replacement if expired."
          }
        }
      },
      "ApprovalMessage": {
        "type": "object",
        "required": [
          "type",
          "event",
          "approved_ids",
          "pending_ids",
          "approved",
          "pending"
        ],
        "description": "Browser postMessage, not an API HTTP response. Check event.origin === https://percardid.com and event.source === the popup. Progress is sent on load and updates; all_approved is sent when none remain pending, followed by popup auto-close after about two seconds. IDs/counts only, no names/photos. Closing early is not approval.",
        "properties": {
          "type": {
            "type": "string",
            "const": "percard:approval"
          },
          "event": {
            "type": "string",
            "enum": [
              "progress",
              "all_approved"
            ]
          },
          "approved_ids": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "pending_ids": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "approved": {
            "type": "integer",
            "minimum": 0
          },
          "pending": {
            "type": "integer",
            "minimum": 0
          }
        }
      },
      "SubmitResponse": {
        "type": "object",
        "required": [
          "data"
        ],
        "properties": {
          "data": {
            "$ref": "#/components/schemas/RowResult"
          },
          "approval": {
            "$ref": "#/components/schemas/Approval"
          }
        }
      },
      "BatchResponse": {
        "type": "object",
        "required": [
          "data",
          "summary"
        ],
        "properties": {
          "data": {
            "type": "array",
            "minItems": 1,
            "maxItems": 100,
            "items": {
              "$ref": "#/components/schemas/RowResult"
            }
          },
          "summary": {
            "type": "object",
            "required": [
              "accepted",
              "failed",
              "held"
            ],
            "properties": {
              "accepted": {
                "type": "integer",
                "minimum": 0,
                "description": "Count of ok:true rows, including duplicates, test-accepted and held rows."
              },
              "failed": {
                "type": "integer",
                "minimum": 0
              },
              "held": {
                "type": "integer",
                "minimum": 0,
                "description": "Held subset of accepted, not an additional count."
              }
            }
          },
          "approval": {
            "$ref": "#/components/schemas/Approval"
          }
        },
        "description": "HTTP 207 even if some or all rows fail. Check every row; successful rows are not rolled back. Destination errors are in data[i].error.code, not a per-row HTTP status."
      },
      "DestinationError": {
        "allOf": [
          {
            "$ref": "#/components/schemas/Error"
          },
          {
            "type": "object",
            "properties": {
              "error": {
                "type": "object",
                "properties": {
                  "code": {
                    "type": "string",
                    "enum": [
                      "form_closed",
                      "form_expired",
                      "order_closed",
                      "destination_full",
                      "no_forms",
                      "destination_closed"
                    ]
                  }
                }
              }
            }
          }
        ],
        "description": "410 means the submission destination cannot accept this cardholder; not a payment/shipping failure or retention deletion. Fix the destination before retrying. These codes can also appear in failed batch rows inside HTTP 207."
      }
    },
    "responses": {
      "DestinationUnavailable": {
        "description": "Submission destination unavailable. Repair the destination before retrying; an unchanged retry will not resolve a 410.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/DestinationError"
            },
            "examples": {
              "form_closed": {
                "summary": "Pinned submission link is not active; reopen it or use an active destination.",
                "value": {
                  "error": {
                    "code": "form_closed",
                    "message": "The form this key sends to is closed and not accepting cardholders."
                  }
                }
              },
              "form_expired": {
                "summary": "Pinned submission link expired; extend it or use an active destination.",
                "value": {
                  "error": {
                    "code": "form_expired",
                    "message": "The form this key sends to has expired. Ask Percard ID to extend it."
                  }
                }
              },
              "order_closed": {
                "summary": "Destination order is missing, archived or no longer accepts submissions; use a new open destination.",
                "value": {
                  "error": {
                    "code": "order_closed",
                    "message": "The order this key sends to has been closed or archived."
                  }
                }
              },
              "destination_full": {
                "summary": "Submission destination closed or hit its limit; review its availability/limit or use a new destination.",
                "value": {
                  "error": {
                    "code": "destination_full",
                    "message": "The form this key sends to is closed or has reached its cardholder limit."
                  }
                }
              },
              "no_forms": {
                "summary": "No usable form; activate one and review the key form access.",
                "value": {
                  "error": {
                    "code": "no_forms",
                    "message": "This key has no active forms to send to. Contact Percard ID."
                  }
                }
              },
              "destination_closed": {
                "summary": "Pinned submission link was deleted; replace the key with one linked to an existing destination.",
                "value": {
                  "error": {
                    "code": "destination_closed",
                    "message": "This key's destination no longer exists. Ask Percard ID for a new key."
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "paths": {
    "/cardholders": {
      "post": {
        "summary": "Add one cardholder",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CardholderIn"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Received, held, or test_accepted. Optional top-level approval for live received rows.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SubmitResponse"
                },
                "examples": {
                  "liveApproval": {
                    "value": {
                      "data": {
                        "ok": true,
                        "id": "00000000-0000-4000-8000-000000000001",
                        "external_id": "EMP-10442",
                        "status": "received",
                        "form": "nurses",
                        "form_version": 1,
                        "order_number": 42
                      },
                      "approval": {
                        "required": true,
                        "url": "https://percardid.com/approve/EXAMPLE_TOKEN",
                        "cards": 1,
                        "expires_at": "2026-12-04T12:00:00.000Z"
                      }
                    }
                  },
                  "testAccepted": {
                    "value": {
                      "data": {
                        "ok": true,
                        "id": "test_EXAMPLE",
                        "external_id": "EMP-10442",
                        "status": "test_accepted",
                        "form": "nurses",
                        "form_version": 1
                      }
                    }
                  },
                  "held": {
                    "value": {
                      "data": {
                        "ok": true,
                        "id": "held_EXAMPLE",
                        "external_id": "EMP-10442",
                        "status": "held",
                        "held_reason": "daily_cap",
                        "form": "nurses",
                        "form_version": 1
                      }
                    }
                  }
                }
              }
            }
          },
          "200": {
            "description": "Existing saved cardholder; optional top-level approval for live duplicate rows. No update performed.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SubmitResponse"
                }
              }
            }
          },
          "422": {
            "description": "Field or photo validation failed",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "410": {
            "$ref": "#/components/responses/DestinationUnavailable"
          },
          "429": {
            "description": "Request-rate limit exceeded; wait and retry with backoff",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "400": {
            "description": "Malformed request or form_required",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Access, scope, IP or account restriction",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Unknown form",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "Conflict or duplicate_external_id race",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Server error; retry later with the same external_id",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "description": "Test keys return 201 test_accepted without persistence or approval links. Live keys return 201 received/held or 200 duplicate. order_number depends on successful automatic order creation. Account order mode and client approval are separate; receipt is not approval or a payment confirmation."
      },
      "get": {
        "summary": "Status changes since a time",
        "parameters": [
          {
            "name": "since",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "date-time"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "maximum": 100,
              "minimum": 1,
              "default": 50
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Statuses",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Status"
                      }
                    },
                    "next_since": {
                      "type": [
                        "string",
                        "null"
                      ]
                    }
                  }
                }
              }
            }
          }
        },
        "description": "Requires cardholders:read. Status-only data, with next_since for polling. No approval state or cardholder personal data."
      }
    },
    "/cardholders/batch": {
      "post": {
        "summary": "Add up to 100",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "cardholders"
                ],
                "properties": {
                  "cardholders": {
                    "type": "array",
                    "maxItems": 100,
                    "items": {
                      "$ref": "#/components/schemas/CardholderIn"
                    },
                    "minItems": 1
                  }
                }
              }
            }
          }
        },
        "responses": {
          "207": {
            "description": "Per-row results, summary and optional approval for accepted/duplicate live rows.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BatchResponse"
                },
                "examples": {
                  "partialDestinationFailure": {
                    "value": {
                      "data": [
                        {
                          "ok": true,
                          "id": "00000000-0000-4000-8000-000000000001",
                          "external_id": "EMP-10442",
                          "status": "received"
                        },
                        {
                          "ok": false,
                          "external_id": "EMP-10443",
                          "error": {
                            "code": "form_expired",
                            "message": "The form this key sends to has expired. Ask Percard ID to extend it."
                          }
                        }
                      ],
                      "summary": {
                        "accepted": 1,
                        "failed": 1,
                        "held": 0
                      }
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Malformed batch; send 1–100 rows",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Access, scope, IP or account restriction",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Request rate exceeded; wait and retry with backoff",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Server error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "description": "Process 1–100 rows. The outer status is 207 even for per-row destination errors. Inspect data[i].ok and error.code; fix and retry only failed rows. Release/discard held rows instead of resubmitting. Request logs retain zero-based failed row indexes and codes, never request payloads."
      }
    },
    "/cardholders/{ref}": {
      "parameters": [
        {
          "name": "ref",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          },
          "description": "Cardholder id, or ext:{external_id}"
        }
      ],
      "get": {
        "summary": "Status of one",
        "responses": {
          "200": {
            "description": "Status",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "$ref": "#/components/schemas/Status"
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Not found"
          }
        },
        "description": "Requires cardholders:read. Use a saved id or URL-encoded ext:{external_id}. Test IDs are not saved. Tracking is a nested tracking object, not top-level tracking_number/tracking_url."
      },
      "delete": {
        "summary": "Remove before production",
        "responses": {
          "200": {
            "description": "Deleted"
          },
          "409": {
            "description": "Already in production or not sent via API"
          }
        },
        "description": "Requires cardholders:write. Only API-sent cards may be removed before production/print jobs. This operation is not a complete account-wide deletion/retention request. Test mode returns a simulated deletion."
      }
    },
    "/forms": {
      "get": {
        "summary": "Forms this key can use, with fields, dropdown choices, photo rule and version",
        "responses": {
          "200": {
            "description": "Forms"
          }
        }
      }
    },
    "/forms/{form}": {
      "parameters": [
        {
          "name": "form",
          "in": "path",
          "required": true,
          "schema": {
            "type": "string"
          }
        }
      ],
      "get": {
        "summary": "One form",
        "responses": {
          "200": {
            "description": "Form"
          },
          "403": {
            "description": "Key not allowed for this form"
          },
          "404": {
            "description": "Unknown form"
          },
          "410": {
            "$ref": "#/components/responses/DestinationUnavailable"
          }
        }
      }
    }
  },
  "x-account-workflow": {
    "ordering": "Account-level batch or one order per cardholder; automatic release and admin intake review are separate controls. Pauses, caps or automatic-order failures leave accepted cards in intake without order_number. Required client approval still blocks printing.",
    "shipping": "Shipping mode and default service are independent of order mode. Use the form address field for individual cardholders. USPS Ground Advantage, USPS Priority Mail, USPS Priority Mail Express, UPS Ground, UPS 3 Day Select, UPS 2nd Day Air, UPS Next Day Air Saver, UPS Next Day Air, or USPS Stamp (untracked, $2 flat). Tracked postage is added at label purchase using actual cost plus configured account markup; stamp postage is added when marked mailed. A default service does not purchase a label or guarantee a rate.",
    "billing": "Optional invoice-each-order-when-shipped; automatic payment depends on account settings and card on file. API acceptance/approval does not confirm an invoice or charge. Card sides and fulfillment extras follow agreed account pricing/production settings.",
    "approval": "No sign-in browser link or portal Saved Cards uses the existing editor, including no-photo cards. Overall badge design approval is a separate Designs decision. Browser preview requires a ready layout; no standalone POST /preview endpoint exists."
  }
}
