> For the complete documentation index, see [llms.txt](https://apidocs.nhanh.vn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://apidocs.nhanh.vn/v3/ship/send.md).

# Gửi đơn

* Gửi đơn hàng đã tạo trước đó sang hãng vận chuyển để lấy mã vận đơn.

## Request

* Xem [common request params](/v3/readme.md#request-params).
* Xem [Postman sample](/v3/ship/send.md).

```curl
curl --location 'https://ship.open.nhanh.vn/v3.0/shipment/send?businessId={{businessId}}&appId={{appId}}' \
--header 'Authorization: {{accessToken}}' \
--header 'Content-Type: application/json' \
--data '{
    "orderId": 12345
}'
```

### Body

| Key     | Type                     | Description                         |
| ------- | ------------------------ | ----------------------------------- |
| orderId | int $$^{\color{red}\*}$$ | ID đơn hàng trong hệ thống từ Nhanh |

## Luồng thực hiện

```
1. Tạo đơn hàng: POST /v3.0/shipment/create (sendCarrier = 0)
   ↓
2. Nhận response với orderId
   ↓
3. Gửi đơn sang hãng: POST /v3.0/shipment/send
   ↓
4. Nhận response với carrierCode (mã vận đơn)
```

## Response

* Xem cấu trúc chung [tại đây](/v3/readme.md#response).

### Failed response

* Xem các mã lỗi chung [tại đây](/v3/readme.md#failed-response).

Các mã lỗi riêng của API này:

| Mã lỗi                        | Ý nghĩa                                           |
| ----------------------------- | ------------------------------------------------- |
| ERR\_INVALID\_DATA            | Không tìm thấy đơn hàng với orderId và businessId |
| ERR\_ORDER\_EXISTED           | Đơn hàng đã được gửi sang hãng trước đó           |
| ERR\_CARRIER\_SEND\_ORDER     | Lỗi khi gửi đơn sang hãng vận chuyển              |
| ERR\_CANNOT\_CONNECT\_CARRIER | Lỗi kết nối với hãng vận chuyển                   |

* Với mã lỗi ERR\_ORDER\_EXISTED thì response sẽ trả về data bao gồm thông tin đơn hàng đã có bên hãng vận chuyển

### Successful response

```json
{
  "code": 1,
  "data": {
    "orderId": "(int) ID đơn hàng trong hệ thống",
    "carrierCode": "(string) Mã vận đơn từ hãng vận chuyển",
    "createdDate": "(string) Ngày tạo đơn (YYYY-MM-DD HH:ii:ss)"
  },
  "messages": [],
  "errorCode": null
}
```

## Ví dụ

### Bước 1: Tạo đơn (không gửi)

```bash
curl --location 'https://ship.open.nhanh.vn/v3.0/shipment/create?businessId={{businessId}}&appId={{appId}}' \
--header 'Authorization: Bearer {{accessToken}}' \
--header 'Content-Type: application/json' \
--data '{
    "businessStoreId": 123,
    "sender": {
        "name": "Nguyễn Văn A",
        "mobile": "0901234567",
        "shippingLocation": {
            "provinceId": 1,
            "wardId": 100,
            "address": "123 Nguyễn Trãi, Hà Nội",
            "longitude": 105.804567,
            "latitude": 21.001234
        }
    },
    "receiver": {
        "name": "Trần Thị B",
        "mobile": "0912345678",
        "shippingLocation": {
            "provinceId": 48,
            "wardId": 12345,
            "address": "456 Lê Lợi, Đà Nẵng"
        }
    },
    "weight": 1500,
    "carrierId": 18,
    "carrierServiceId": 100,
    "totalFee": 25000,
    "shipFee": 25000,
    "customerShipFee": 22000,
    "totalCod": 650000,
    "sendCarrier": 0
}'
```

**Response:**

```json
{
  "code": 1,
  "data": {
    "orderId": 12345,
    "createdDate": "2024-08-25 10:30:00",
    "appId": 1
  },
  "messages": [],
  "errorCode": null
}
```

### Bước 2: Gửi đơn sang hãng

```bash
curl --location 'https://ship.open.nhanh.vn/v3.0/shipment/send?businessId={{businessId}}&appId={{appId}}' \
--header 'Authorization: Bearer {{accessToken}}' \
--header 'Content-Type: application/json' \
--data '{
    "orderId": 12345
}'
```

**Response:**

```json
{
  "code": 1,
  "data": {
    "orderId": 12345,
    "carrierCode": "AHM-2024082512345678",
    "createdDate": "2024-08-25 10:30:00"
  },
  "messages": [],
  "errorCode": null
}
```
