# POST /api/v1/credit/programs/{program_id}/loans/import/from-partner-pull

> Build and stage the migration file from a stored pull

- **Tag:** credit
- **Operation ID:** `credit_migration_build_from_pull`

## Description

Sandbox only: pulls are kept on a sandbox business (`CREDIT_090` elsewhere), so this is the console's rehearsal of the cut-over; the live book is built from the same files by an operator script. Reads the pull's merchants and transactions tables and builds the launch import (Appendix E): one row per loan with a balance, keyed on the Brex transaction id; its paid amounts are what Tie's system assessed less what it reports outstanding, per loan or from the merchant's balance split over its loans oldest first (D-58); its interest good through `pull_date` and, above the program's ceiling, loaded at the ceiling (D-59). With `since`, the tail: the transactions that posted from that date, loaded as they posted (D-60). The file is staged on the program's loan import, and `run` is what to send with `staging_id` to `POST /credit/programs/{id}/loans/import` for the dry run and the apply. `report` is the exceptions report: rows refused and why, the lines and which are not open yet, interest reduced to the ceiling, loans repaid before cut-over (phase 2), and the control totals. `open_lines` also opens each merchant's line with its own terms, a merchant with no balance included, and sets its email, legal name and address; send it before the apply. `400` `CREDIT_770` when `since` is after `pull_date`, `CREDIT_771` when nothing would be imported, `CREDIT_772` for a mapping field the builder does not read, `CREDIT_773` for a cut-over pull with no merchants table.

## Authentication

Bearer token in `Authorization` header.
Required header: `x-business-id: <business uuid>`.

## Parameters

- `program_id` (path, string, required) — The program to stage the file on.

## Request body

Schema: `MigrationFromPullRequest`

- `pull_id` (string · required) — The pull (`GET /credit/sandbox/partner-pulls`).
- `pull_date` (string · date · required) — The day the pull is as at the close of. Every row's interest is good through it (`accrued_through`, D-59), and the import runs as at the day after.
- `since` (string · date) — The tail (D-60): only transactions that posted on or after this date, each loaded as it posted. Null for the cut-over import.
- `roles` (object) — Role → table name, overriding the pull's: `transactions`, `merchants`.
- `mapping` (MigrationFieldMapping | string) — The partner's field names, laid over the defaults; `auto` works them out from the tables (a money field only on a header that is one of its names).
- `balance_basis` (string) (one of: auto, loan, merchant) — Where the outstanding figures are read (D-58): `loan` from each transaction, `merchant` from the merchant's balance (split over its loans oldest first), `auto` per loan for a merchant whose every loan carries a figure, else the merchant's.
- `line_key` (string) (one of: external_line_id, external_merchant_id) — Which line id carries Tie's merchant id. `external_line_id` (the default) opens each line under it, idempotently.
- `open_lines` (boolean) — Also open each merchant's line with its own terms, and set its email, legal name and address from the merchants table (as `PATCH /credit/lines/{id}` does). Off: nothing is written but the staged file, and `run` opens placeholder lines inside the dry run.

## Responses

### 201 — Successful Response

Schema: `SuccessEnvelope_MigrationBuildResponse_`

- `success` (boolean)
- `message` (string)
- `code` (string)
- `data` (MigrationBuildResponse)

### 400 — Bad Request - Invalid input

Schema: `ErrorResponse`

- `error` (string · required) — Error category: `validation_error`, `unauthorized`, `forbidden`, `not_found`, `conflict`, `rate_limited`, `server_error`, or `service_unavailable`.
- `message` (string · required) — Human-readable explanation, safe to show to end users.
- `code` (string · required) — Stable machine-readable code in `CATEGORY_NNN` form (e.g. `NOT_FOUND_006`, `AUTH_010`, `GEN_002`).
- `request_id` (string) — Correlation id for support requests. Echoes the `x-request-id` request header when one was supplied.
- `errors` (object) — Field-level validation messages keyed by field name. Present on 400/422 validation failures only.

### 401 — Unauthorized - Missing or expired token, an MCP OAuth token used against the REST API, or no access to this business

Schema: `ErrorResponse`

- `error` (string · required) — Error category: `validation_error`, `unauthorized`, `forbidden`, `not_found`, `conflict`, `rate_limited`, `server_error`, or `service_unavailable`.
- `message` (string · required) — Human-readable explanation, safe to show to end users.
- `code` (string · required) — Stable machine-readable code in `CATEGORY_NNN` form (e.g. `NOT_FOUND_006`, `AUTH_010`, `GEN_002`).
- `request_id` (string) — Correlation id for support requests. Echoes the `x-request-id` request header when one was supplied.
- `errors` (object) — Field-level validation messages keyed by field name. Present on 400/422 validation failures only.

### 403 — Forbidden - Insufficient permissions

Schema: `ErrorResponse`

- `error` (string · required) — Error category: `validation_error`, `unauthorized`, `forbidden`, `not_found`, `conflict`, `rate_limited`, `server_error`, or `service_unavailable`.
- `message` (string · required) — Human-readable explanation, safe to show to end users.
- `code` (string · required) — Stable machine-readable code in `CATEGORY_NNN` form (e.g. `NOT_FOUND_006`, `AUTH_010`, `GEN_002`).
- `request_id` (string) — Correlation id for support requests. Echoes the `x-request-id` request header when one was supplied.
- `errors` (object) — Field-level validation messages keyed by field name. Present on 400/422 validation failures only.

### 404 — Not Found - Resource does not exist

Schema: `ErrorResponse`

- `error` (string · required) — Error category: `validation_error`, `unauthorized`, `forbidden`, `not_found`, `conflict`, `rate_limited`, `server_error`, or `service_unavailable`.
- `message` (string · required) — Human-readable explanation, safe to show to end users.
- `code` (string · required) — Stable machine-readable code in `CATEGORY_NNN` form (e.g. `NOT_FOUND_006`, `AUTH_010`, `GEN_002`).
- `request_id` (string) — Correlation id for support requests. Echoes the `x-request-id` request header when one was supplied.
- `errors` (object) — Field-level validation messages keyed by field name. Present on 400/422 validation failures only.

### 422 — Validation Error

Schema: `ErrorResponse`

- `error` (string · required) — Error category: `validation_error`, `unauthorized`, `forbidden`, `not_found`, `conflict`, `rate_limited`, `server_error`, or `service_unavailable`.
- `message` (string · required) — Human-readable explanation, safe to show to end users.
- `code` (string · required) — Stable machine-readable code in `CATEGORY_NNN` form (e.g. `NOT_FOUND_006`, `AUTH_010`, `GEN_002`).
- `request_id` (string) — Correlation id for support requests. Echoes the `x-request-id` request header when one was supplied.
- `errors` (object) — Field-level validation messages keyed by field name. Present on 400/422 validation failures only.

## Code samples

### cURL

```bash
curl -X POST 'https://api.ondayzero.com/api/v1/credit/programs/{program_id}/loans/import/from-partner-pull' \
  -H 'Authorization: Bearer dz_your_token' \
  -H 'x-business-id: YOUR_BUSINESS_ID' \
  -H 'Content-Type: application/json' \
  -d '{
  "pull_id": "string",
  "pull_date": "2026-01-01"
}'
```

### JavaScript

```javascript
const response = await fetch('https://api.ondayzero.com/api/v1/credit/programs/{program_id}/loans/import/from-partner-pull', {
  method: 'POST',
  headers: {
    Authorization: 'Bearer dz_your_token',
    'x-business-id': 'YOUR_BUSINESS_ID',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
  "pull_id": "string",
  "pull_date": "2026-01-01"
}),
});
const data = await response.json();
```

### Python

```python
import httpx

headers = {
    "Authorization": "Bearer dz_your_token",
    "x-business-id": "YOUR_BUSINESS_ID",
}

payload = {'pull_id': 'string', 'pull_date': '2026-01-01'}

response = httpx.post("https://api.ondayzero.com/api/v1/credit/programs/{program_id}/loans/import/from-partner-pull", headers=headers, json=payload)
data = response.json()
```

## See also

- HTML version: https://www.ondayzero.com/docs/reference/credit/credit-migration-build-from-pull
- OpenAPI slice: https://www.ondayzero.com/docs/reference/credit/credit-migration-build-from-pull/openapi.json
- Other endpoints in **credit**: https://www.ondayzero.com/docs/reference/credit (markdown bundle: https://www.ondayzero.com/docs/reference/credit.md, OpenAPI: https://www.ondayzero.com/docs/reference/credit/openapi.json)
- Endpoint catalog: https://www.ondayzero.com/docs/reference/index.md
