# POST /api/v1/credit/programs/{program_id}/loans/import

> Dry-run or apply a staged loan file across the program

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

## Description

Fans the staged rows out to each line's loan import (1,000 rows per call) under one savepoint. With `dry_run` (the default), or any control total that does not match across the whole file, nothing is written, not one line; otherwise every line is written together. `create_missing_lines` opens a line for each key the program lacks, with `line_defaults`, through the ordinary line opening (so the program's limit envelope applies); a refusal rejects that line's rows. `mode` says where it runs: in the request (`201` with the per-line results, the totals against the control totals, and the report id), or as a background job (`202` with the batch, `status` `running`; poll `GET /credit/import-batches/{id}` for its progress and, when it finishes, the same result). `auto`, the default, runs up to 1,000 rows in the request. One run at a time per program. Operator action: an API token receives `403`.

## Authentication

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

## Parameters

- `program_id` (path, string, required)

## Request body

Schema: `ProgramLoanImportRequest`

- `staging_id` (string · required) — The batch from the parse step.
- `dry_run` (boolean) — Run every line and report, then write nothing. On by default.
- `as_of` (string · date) — Loan status is assessed as at this date. Today.
- `expected_count` (integer) — Rows the source file holds. A different count writes nothing.
- `expected_principal_cents` (integer) — Original principal summed over the file.
- `expected_outstanding_cents` (integer) — Outstanding summed over the file: principal, fee and accrued interest, less what was paid or credited; written-off loans count as zero.
- `create_missing_lines` (boolean) — Open a line for each key the program does not have yet.
- `line_defaults` (ImportLineDefaults) — Terms for the lines `create_missing_lines` opens.
- `mode` (string) (one of: sync, async, auto) — `sync` runs the file in the request and answers with the result. `async` queues it as a background job and answers `202` with the batch (`status: running`); poll `GET /credit/import-batches/{id}`. `auto` (the default) runs files of up to 1,000 rows in the request and larger ones as a job. `sync` is refused over 2,000 rows.
- `allow_cross_line_duplicates` (boolean) — A row whose original transaction id is already on a different line of the program is rejected (most likely a loan imported before under another merchant key), on a dry run as on an apply. Set true to load such rows anyway; each is listed in `warnings`.
- `dry_run_token` (string) — On an apply: the `dry_run_token` of the dry run you checked. The apply then runs only with exactly that dry run's inputs (`as_of`, the expected totals, `create_missing_lines`, `line_defaults`) under the mapping it ran with, and it must be the batch's last dry run; otherwise `400` `CREDIT_690` and nothing runs. Optional.

## Responses

### 201 — Run in the request: the per-line results, the totals against the control totals, and the report id.

Schema: `SuccessEnvelope_ProgramLoanImportResponse_`

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

### 202 — Queued as a background job: the batch, `status` `running`.

Schema: `SuccessEnvelope_ImportBatchResponse_`

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

### 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' \
  -H 'Authorization: Bearer dz_your_token' \
  -H 'x-business-id: YOUR_BUSINESS_ID' \
  -H 'Content-Type: application/json' \
  -d '{
  "staging_id": "string"
}'
```

### JavaScript

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

### Python

```python
import httpx

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

payload = {'staging_id': 'string'}

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

## See also

- HTML version: https://www.ondayzero.com/docs/reference/credit/credit-import-run-loans
- OpenAPI slice: https://www.ondayzero.com/docs/reference/credit/credit-import-run-loans/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
