Shipment API

Send shipments to the CRM, update status, list and delete them (/shipments). Retries never create duplicates.

# Shipment API (`/shipments`)

Send, update, list and delete shipments from your own system (e-commerce, ERP, warehouse software) in MyVoipCRM. Records appear on the CRM **Shipments** screen, and the **shipment automations** defined in the CRM (SMS, WhatsApp template, e-mail, notification, webhook) run on status changes.

Base URL: `https://<api-host>/api/external/v1/shipments`
Authentication: an `X-Api-Key` header on every request plus an allowed IP defined in the CRM (see *Authentication* in the main document).

## Prerequisites

- The **Shipping** module must be enabled for your company. Otherwise every `/shipments` endpoint returns `403` with `MODULE_NOT_ENABLED`.
- The **External API** module and an API key must be set up.

## Addressing a record

| Route | Example |
|---|---|
| CRM ID | `/shipments/42` |
| Your own ID (`externalId`) | `/shipments/by-external-id/ORD-1001` |

`externalId` is unique within your company and matched case-insensitively (`ORD-1001` = `ord-1001`). The `externalId` of a deleted shipment can be reused. **Always send an `externalId` so that retries never create duplicates.**

## Endpoints

| Method | Path | Description |
|---|---|---|
| `GET` | `/shipments` | List (filtered, paged) |
| `GET` | `/shipments/meta` | Value lists for status, carrier, etc. |
| `GET` | `/shipments/{id}` · `/shipments/by-external-id/{externalId}` | Single record |
| `POST` | `/shipments` | Create; update if the `externalId` already exists |
| `POST` | `/shipments/bulk-upsert` | Create / update up to 100 records in one request |
| `PUT` | `/shipments/{id}` · `/shipments/by-external-id/{externalId}` | Replace all fields |
| `PATCH` | `/shipments/{id}` · `/shipments/by-external-id/{externalId}` | Change only the fields sent |
| `PATCH` | `/shipments/{id}/status` · `/shipments/by-external-id/{externalId}/status` | Status and delivery info |
| `DELETE` | `/shipments/{id}` · `/shipments/by-external-id/{externalId}` | Delete (moves to the CRM trash) |

## Fields

| Field | Type | Description |
|---|---|---|
| `externalId` | string (≤100) | Your shipment ID |
| `customerId` | int | CRM customer ID. When set, `customer` is ignored |
| `customer` | object | Find or create the customer: `{ "externalId", "id", "create": { "fullName", "phoneNumber1", "email", ... } }`. Looked up by externalId → e-mail → phone; created from `create` if not found |
| `status` | enum | Shipment status |
| `shipmentType` | enum | Shipment type |
| `shipmentCategory` | enum | Package category |
| `paymentType` | enum | Payment type |
| `direction` | enum | Incoming / outgoing |
| `carrier` | enum | Carrier |
| `trackingNumber` | string (≤128) | Tracking number |
| `price` | number | Price (≥ 0, rounded to 2 decimals) |
| `shipmentDate` | date | Ship date |
| `deliveredDate` | date | Delivery date |
| `receivedByPersonName` | string (≤256) | Person who received the parcel |
| `receivedByUserId` | string | CRM staff user who received the parcel (must belong to your company) |
| `description`, `note` | string (≤4000) | Description, note |

**Enums** may be sent as a name (`"Delivered"`) or a number (`4` / `"4"`); responses always contain the name. See `GET /shipments/meta` for every value with its Turkish label.

**Dates** are ISO 8601. With a time zone (`Z` or `+03:00`) it is honoured; without one, **Turkey time (UTC+3)** is assumed. Dates in responses are always UTC (`Z`).

### Rules

- If the status is `Delivered` and `deliveredDate` is empty, the **request time** is stored as the delivery date.
- If the status is `Received`, `receivedByPersonName` is required.

### Status values (`status`)

| Value | Name | Turkish label |
|---|---|---|
| 0 | `Pending` | Bekliyor |
| 1 | `Prepared` | Hazırlanıyor |
| 2 | `Shipped` | Kargoya Verildi |
| 3 | `InTransit` | Yolda |
| 4 | `Delivered` | Teslim Edildi |
| 5 | `Returned` | İade Edildi |
| 6 | `Cancelled` | İptal Edildi |
| 7 | `Received` | Teslim Alındı |
| 8 | `Lost` | Kayıp |
| 9 | `Problematic` | Sorunlu |
| 10 | `NotificationForm` | Haber Formu |
| 11 | `ReturnInProgress` | İade Sürecinde |
| 12 | `RedeliveryScheduled` | Tekrar Dağıtım Planlandı |
| 13 | `AtDeliveryBranch` | Teslimat Şubesinde |
| 14 | `OutForDelivery` | Kurye Dağıtımda |
| 15 | `DeliveryFailed` | Teslim Edilemedi |
| 16 | `AcceptedByCarrier` | Kabul Edildi |

### Carriers (`carrier`)

`Undefined` (0), `ArasKargo` (1), `YurticiKargo` (2), `MngKargo` (3), `PttKargo` (4), `SuratKargo` (5), `Ups` (6), `Dhl` (7), `FedEx` (8), `HorozLojistik` (9), `CevaLojistik` (10), `KolayGelsin` (11), `HepsiJet` (12), `TrendyolExpress` (13), `Scotty` (14), `EverestKargoPtt` (15), `FestKargo` (16).

### Shipment type (`shipmentType`)

| Value | Name | Turkish label |
|---|---|---|
| 0 | `Free` | Ödemesiz |
| 1 | `Paid` | Ücretli |
| 2 | `CashOnDelivery` | Kapıda Ödeme |
| 3 | `PostOfficeCard` | Postane Kart |
| 4 | `PostOfficeCash` | Postane Nakit |
| 5 | `ArasCard` | Aras Kart |
| 6 | `ArasCash` | Aras Nakit |

### Package category (`shipmentCategory`)

| Value | Name | Turkish label |
|---|---|---|
| 0 | `Undefined` | Belirtilmemiş |
| 1 | `Box` | Koli |
| 2 | `Envelope` | Zarf |
| 3 | `File` | Dosya |
| 4 | `Magazine` | Dergi |
| 5 | `Campaign` | Şampanya |
| 6 | `Newspaper` | Gazete |
| 7 | `Document` | Doküman |
| 8 | `BusinessCard` | Kartvizit |
| 9 | `OfficialNotice` | Teblig Zarfı |
| 10 | `Flower` | Çiçek |
| 11 | `Other` | Diğer |
| 12 | `OfficialDocument` | Resmi Evrak |
| 13 | `BankCard` | Banka Kartı |

### Payment type (`paymentType`)

| Value | Name | Turkish label |
|---|---|---|
| 0 | `Undefined` | Belirtilmemiş |
| 1 | `SenderPays` | Gönderici Öder |
| 2 | `ReceiverPays` | Alıcı Öder |

### Direction (`direction`)

| Value | Name | Turkish label |
|---|---|---|
| 0 | `Undefined` | Belirtilmemiş |
| 1 | `Incoming` | Gelen Kargo |
| 2 | `Outgoing` | Giden Kargo |

The same lists are also available from `GET /shipments/meta`.

## `POST /shipments` — Create / update

```http
POST /api/external/v1/shipments
X-Api-Key: crm_xxx
Content-Type: application/json

{
  "externalId": "ORD-1001",
  "customer": {
    "externalId": "CUST-77",
    "create": { "fullName": "Jane Doe", "phoneNumber1": "5321112233", "email": "[email protected]" }
  },
  "status": "Shipped",
  "carrier": "ArasKargo",
  "direction": "Outgoing",
  "trackingNumber": "ARS123456",
  "price": 49.90,
  "shipmentDate": "2026-10-19T10:00:00+03:00",
  "note": "Leave at the door"
}
```

- If no record has this `externalId`, it is created (`wasUpdated: false`).
- If one exists, only the fields you send (non-null) are updated (`wasUpdated: true`). Retrying the same request is safe.
- `customerCreated: true` when a new customer was created.

The response `data` has the same shape as `GET /shipments/{id}`: `id`, `externalId`, `customerId`, `customerFullName`, `customerExternalId`, `status`, `statusText`, `shipmentType`, `shipmentCategory`, `paymentType`, `direction`, `carrier`, `carrierText`, `trackingNumber`, `price`, `shipmentDate`, `deliveredDate`, `receivedByPersonName`, `receivedByUserId`, `description`, `note`, `carrierShipmentId`, `carrierLastSyncedAt`, `hasAttachment`, `createdAt`, `updatedAt`, `wasUpdated`, `customerCreated`.

`carrierShipmentId` / `carrierLastSyncedAt` are filled when the shipment was dispatched through the CRM's own carrier integration (Aras, Yurtiçi, Everest, Fest); they are read-only.

## `PATCH /shipments/by-external-id/{externalId}/status` — Update status

The most frequently used endpoint. Fields not sent are left unchanged.

```json
{ "status": "Delivered", "deliveredDate": "2026-10-20T14:30:00+03:00" }
```

| Field | Required | Notes |
|---|---|---|
| `status` | yes | New status |
| `trackingNumber` | no | |
| `deliveredDate` | no | Request time if `Delivered` and empty |
| `receivedByPersonName` | yes for `Received` | |
| `note` | no | **Replaces** the note |

## `PATCH` vs `PUT`

- **PATCH**: only fields present in the body change. Send `null` to clear a field: `{ "note": null }`.
- **PUT**: the record is **fully** replaced; fields missing from the body are cleared (including the customer). When called via `by-external-id` without an `externalId` in the body, the existing ID is kept.

`externalId` can be changed with PATCH / PUT; if another shipment already uses it you get `409 EXTERNAL_ID_CONFLICT`.

## `POST /shipments/bulk-upsert`

```json
{
  "items": [
    { "externalId": "ORD-1001", "status": "Delivered" },
    { "externalId": "ORD-1002", "status": "Shipped", "carrier": "YurticiKargo", "trackingNumber": "YK998877" }
  ]
}
```

- Up to **100** items; `externalId` is required on each.
- Each item is processed like `POST /shipments`, independently: a failing item does not stop the others.
- Returns HTTP `200`; per-item results are in `results` (`index`, `externalId`, `success`, `id`, `wasUpdated`, `code`, `message`) with totals `total`, `created`, `updated`, `failed`.

## `GET /shipments` — List

Most recently updated first. `page` (default 1), `pageSize` (default 20, max 100).

| Parameter | Description |
|---|---|
| `externalId` / `externalIds` | One or many (repeated parameter or comma-separated; max 100) |
| `customerId` / `customerExternalId` | By customer |
| `status`, `carrier`, `direction` | Name or number |
| `trackingNumber` | Exact match |
| `shipmentFrom` / `shipmentTo` | Ship date range |
| `updatedFrom` / `updatedTo` | Last update range |

Date ranges may span at most **90 days**.

**Pulling changes made in the CRM:** CRM users or automations may update a shipment. Poll `GET /shipments?updatedFrom=<last poll time>` to fetch everything changed since your last call.

## `DELETE`

The record is moved to the CRM trash (`200`). "Shipment deleted" automations run, and leads linked through the pending-shipment list are unlinked.

## Error codes

| HTTP | `code` | When |
|---|---|---|
| 400 | `VALIDATION_FAILED` | Invalid enum / date / length, rule violation (e.g. no person name for `Received`) |
| 400 | `INVALID_REQUEST` | Body is not a JSON object |
| 400 | `INVALID_PAGINATION` | Invalid `page` / `pageSize` |
| 400 | `USER_NOT_FOUND` | `receivedByUserId` does not belong to your company |
| 400 | `TOO_MANY_ITEMS` | More than 100 items in a bulk request |
| 401 | `INVALID_API_KEY` | Missing / invalid key |
| 403 | `IP_NOT_ALLOWED` | IP not allowed |
| 403 | `MODULE_NOT_ENABLED` | Shipping module disabled |
| 404 | `RECORD_NOT_FOUND` | Shipment not found |
| 404 | `CUSTOMER_NOT_FOUND` | `customerId` / `customer.id` not found |
| 409 | `EXTERNAL_ID_CONFLICT` | `externalId` used by another shipment |
| 429 | `RATE_LIMIT_EXCEEDED` | Per-minute request limit exceeded |
| 500 | `UNEXPECTED_ERROR` | Server error |

In bulk requests, item errors are returned in `results[].code`, including `EXTERNAL_ID_REQUIRED`.