Migrate Documents Using the API
Use an API-led migration when your application needs to control extraction, transformation, writes, checkpoints, and verification. This walkthrough uses the same three documents and expected metadata as the CLI walkthrough.
The downloadable example requests presigned upload URLs through the API and sends file bytes directly to storage. Its request shapes were checked against the v1.19.2 source. It was tested with an isolated HTTP fixture, including lost create and upload responses, interrupted batches, required upload headers, paginated attributes, and corrupted downloads. A live migration still needs testing against your deployed version, permissions, schemas, and processing rules.
1. Prepare the shared sample and API access
Complete Prepare a Migration Sample. Keep migration_sample.py beside the prepared directory and retain the approved FORMKIQ_SITE_ID from that step. Set the API base URL for JWT authentication, without a trailing slash:
export FORMKIQ_API_URL="https://YOUR_JWT_API_BASE_URL"
Obtain a JWT through the API walkthrough. Read it into the shell without echoing it or putting its value in the command history:
read -r -s FORMKIQ_TOKEN
export FORMKIQ_TOKEN
Paste the token and press Enter when running read. The script sends that value in the API Authorization header. It does not sign IAM requests. Use the CLI's AWS profile or an appropriate signing client when calling an IAM-authenticated API.
2. Inspect the API sequence
The example uses these operations:
| Operation | Role in the sample |
|---|---|
| GET /attributes/<key> and POST /attributes | Confirm the three sample definitions are compatible, creating missing STRING definitions. |
| GET /documents/<documentId> | Check a mapped ID before writing and read back the resulting path and content type. |
| POST /documents/upload | Register the prepared UUID, path, content type, attributes, and SHA-256 checksum; receive a presigned upload URL and required headers. |
PUT to the returned storage URL | Send the original file bytes with the returned headers and content type. This request carries no FormKiQ JWT. |
| GET /documents/<documentId>/upload | Obtain a fresh upload URL when resuming a record created by this job whose content upload was never attempted. |
| GET /documents/<documentId>/attributes | Read all attribute pages and compare the required values. |
| GET /documents/<documentId>/url | Obtain a temporary content URL, then download bytes in a separate request. |
The script sends the prepared documentId explicitly. A path or source attribute alone does not make a create request idempotent. It verifies the source/batch mapping before resuming an unstarted upload, and stops on existing content or metadata that differs from the sample.
The upload request contains metadata and the checksum. The file bytes travel in the separate storage PUT. The script passes through the returned checksum headers, including x-amz-checksum-sha256 when supplied, and adds the content type if it is not already present.
3. Import a bounded first batch
python3 migration_sample.py import --directory migration-sample --max-records 1
This checks or creates the three attribute definitions and imports the first document. The script atomically saves progress in migration-sample/api-checkpoint.json: create-attempted before requesting the upload URL, created after receiving it, upload-attempted before sending bytes, and uploaded after storage acknowledges the PUT. It records verified only after reading back the document, attributes, and matching content bytes.
Expect one Verified legacy-001 -> ... line. The checkpoint binds the job to the manifest checksum, API URL, and site. It contains no JWT or presigned content URL. Run one importer process per prepared directory; this example does not coordinate concurrent workers.
4. Resume the remaining records
python3 migration_sample.py import --directory migration-sample
The script verifies the first mapped document again, then imports and verifies the remaining two. Running it again reads and verifies all three without creating new documents when the stored content and required metadata still match.
The three Verified lines show the checks passed for this prepared batch. They do not establish a complete source inventory or completion of indexing, OCR, workflows, or other asynchronous processing.
Resume or reconcile
The example retries reads for selected transient errors with bounded backoff. It attempts each create and storage PUT once before reconciling their outcomes. The checkpoint records the document ID and stage, while temporary upload URLs remain only in memory.
On the next run:
- If the document exists and its path, content type, required attributes, and bytes match, verification completes and the job continues.
- If the job registered the document but never attempted its upload, it checks the source/batch attributes and existing content first. If the content is absent, it requests a fresh upload URL with the original SHA-256 checksum and uploads the file.
- If an upload response was lost after storage accepted the bytes, matching content is verified without another
PUT. - If an upload was attempted but its outcome remains unresolved after the read checks, the script stops. It does not assume that missing content means a re-upload is safe.
- If a previous create is unresolved and the ID is absent, the script stops without replaying the create. Confirm the outcome through the API and operational evidence before approving another write.
- If content or metadata differs, the script stops without overwriting it. Investigate a changed source, concurrent edits, wrong mapping, or an incomplete previous operation.
- If a previously verified record has disappeared, the script stops instead of recreating it.
Preserve the original checkpoint before any manual repair. Only clear an affected create entry after establishing that the earlier create was not accepted. Only return an upload entry to created after confirming that a new content write is appropriate. Re-uploading can create a version or repeat processing. Do not delete the entire checkpoint or prepare a fresh set of IDs to clear an error.
An expired JWT can be refreshed in the shell without changing the checkpoint. A different API, site, or manifest requires a separate reviewed migration job.
Large files and production migration jobs
The sample uses the presigned upload flow for all three files. It retains a 1 MiB per-file guard to keep the example's in-memory transfers bounded; this is not a FormKiQ upload limit. For larger migration files, adapt the transfer and checksum code to stream content, and choose the supported upload method for the file size. Reconcile timed-out uploads before repeating them, and keep the FormKiQ JWT on API requests.
Before adapting the example for production, add the source-specific transformations, supported metadata types, version and artifact mapping, controlled concurrency, processing checks, and change reconciliation required for your repository. Confirm limits and pagination, and avoid treating an insertion-ordered listing as an update log.
Finish with Export and Verify a Migration Sample, or return to the shared migration planning guide.