> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://developers.omicx.one/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://developers.omicx.one/_mcp/server.

# Tạo khách hàng

POST https://api.omicx.one/customers
Content-Type: application/json

**Scope yêu cầu:** `customer:write`. Agent gắn với key cần quyền tạo (`crm.customer` → create). Response chỉ trả về `customerId` vừa tạo — gọi [Chi tiết khách hàng](/customers/info/get-customer) để lấy đầy đủ dữ liệu.

Reference: https://developers.omicx.one/customers/info/create-customer

## Request

### Headers

- `X-Api-Key` (string, required)

### Body (application/json)

This endpoint expects a CustomerCreateRequest.

- `lifecycleStages` (string, optional) — Mã giai đoạn hành trình — tenant có thể tự cấu hình (xem [Cấu hình khách hàng](/customer-configuration) → Hành trình). Mẫu mặc định: `subscriber` (Đăng ký), `lead` (Tiềm năng), `qualifiedLead` (Đủ điều kiện), `opportunity` (Cơ hội bán hàng), `official` (Chính thức), `evangelist` (Trung thành), `other` (Khác).
- `status` (string, optional) — Mã trạng thái khách hàng — tenant có thể tự cấu hình (xem [Cấu hình khách hàng](/customer-configuration) → Trạng thái). **Tuỳ chọn khi tạo mới** — bỏ trống sẽ lấy trạng thái đầu tiên theo cấu hình của tenant. Mẫu mặc định: `active` (Hoạt động), `inactive` (Không hoạt động), `atRisk` (Rủi ro), `lost` (Rời bỏ).
- `fullName` (string, optional)
- `phone` (string, optional) — Định dạng E.164.
- `email` (string, optional)
- `dateOfBirth` (datetime, optional)
- `gender` (string, optional) — Mã giá trị lựa chọn của thuộc tính Giới tính.
- `customerSource` (string, optional) — Mã giá trị lựa chọn của thuộc tính Nguồn khách hàng.
- `description` (string, optional) — Ghi chú.
- `avatarUrl` (string, optional)
- `attributes` (map from string to any, optional) — Thuộc tính tuỳ chỉnh của tenant, khoá theo **fieldCode** (mã thuộc tính — không phải slot lưu trữ nội bộ `valueKey`). Mã không tồn tại hoặc gửi thẳng slot thô → `422 CUSTOMER_ATTRIBUTE_UNKNOWN`.
- `socialIds` (map from string to string, optional)
- `assignedAgentId` (string, optional) — uid của agent phụ trách. Bỏ trống khi tạo mới = khách hàng chưa gán cho ai (key của agent có phạm vi dữ liệu MINE/GROUP sau đó sẽ không đọc lại được khách hàng này).
- `relatedAgentIds` (list of string, optional)
- `relatedCustomerUids` (list of string, optional)
- `tagIds` (list of string, optional)
- `groupIds` (list of string, optional)
- `refId` (string, optional) — Mã tham chiếu ngoài.

## Response

### 201

Đã tạo — `{customerId, success}`.

- `customerId` (string, optional)
- `success` (boolean, optional)

## Errors

### 400 Bad Request Error

Lỗi dữ liệu đầu vào, ví dụ `CUSTOMER_FIELD_UNKNOWN` — gửi field không tồn tại trong `CustomerWriteFields`.

- `error` (ErrorResponseError, optional)

### 401 Unauthorized Error

`API_KEY_REQUIRED` hoặc `API_KEY_INVALID`.

- `error` (ErrorResponseError, optional)

### 403 Forbidden Error

`INSUFFICIENT_SCOPE` (key thiếu scope) · `API_KEY_PRINCIPAL_REQUIRED` (legacy key chưa gắn agent — tạo key mới) · `API_KEY_PRINCIPAL_NOT_ELIGIBLE` (agent của key đã bị vô hiệu hoá, rời tenant, hoặc không còn thuộc tenant) · `FEATURE_ACCESS_DENIED` (quyền hiện tại của agent không cho phép thao tác này trên Khách hàng) · `CUSTOMER_ACCESS_DENIED` (ngoài phạm vi dữ liệu của agent).

- `error` (ErrorResponseError, optional)

### 409 Conflict Error

Trùng khách hàng, theo chính sách chống trùng lặp của tenant (xem [Cấu hình khách hàng](/customer-configuration)).

- `error` (ErrorResponseError, optional)

### 422 Unprocessable Entity Error

`CUSTOMER_ATTRIBUTE_UNKNOWN` — một key trong `attributes` không phải fieldCode đang hoạt động (bao gồm cả trường hợp gửi thẳng slot lưu trữ thô).

- `error` (ErrorResponseError, optional)

### 429 Too Many Requests Error

`RATE_LIMIT_EXCEEDED`.

- `error` (ErrorResponseError, optional)

### 502 Bad Gateway Error

`CUSTOMER_SERVICE_UNAVAILABLE` — customer-service timeout/lỗi 5xx, kèm `Retry-After: 5`. Platform không tự động thử lại.

- `error` (ErrorResponseError, optional)

### 503 Service Unavailable Error

`AGENT_SERVICE_UNAVAILABLE` / `PERMISSION_SNAPSHOT_UNAVAILABLE` / `SCOPE_RESOLUTION_FAILED` — không xác thực được quyền hạn, hệ thống từ chối theo hướng an toàn (fail closed).

- `error` (ErrorResponseError, optional)

## Types

### ErrorResponseError

- `code` (string, optional) — Nhóm lỗi (dạng máy đọc được), ổn định theo từng HTTP status.
- `subCode` (string, optional, nullable) — Mã cụ thể, ổn định — dùng để phân nhánh xử lý / dịch i18n. Hãy xử lý logic dựa trên `subCode`, không dựa vào chuỗi `message`.
- `message` (string, optional) — Mô tả cho con người đọc (dùng để debug), không nên dịch trực tiếp.
- `timestamp` (long, optional) — Epoch millis.
- `path` (string, optional) — Đường dẫn request đầy đủ.
- `traceId` (string, optional, nullable) — Mã tương quan request, có khi lỗi đi qua bộ xử lý lỗi chung.
- `details` (list of ErrorResponseErrorDetailsItems, optional, nullable) — Chỉ xuất hiện khi `code` là `VALIDATION_ERROR` — danh sách lỗi theo từng trường.

### ErrorResponseErrorDetailsItems

- `field` (string, optional)
- `message` (string, optional)
- `rejectedValue` (any, optional, nullable)

## Examples

**Request**

```json
{
  "status": "active",
  "fullName": "Nguyễn Văn A",
  "phone": "+84912345678",
  "email": "a@example.com",
  "customerSource": "web-client",
  "attributes": {
    "cap_bac": "VIP",
    "diem_tich_luy": 120
  }
}
```

**Response**

```json
{
  "customerId": "K7Q2M9X4P1A",
  "success": true
}
```

**SDK Code**

```python Tạo khách hàng mới
import requests

url = "https://api.omicx.one/customers"

payload = {
    "status": "active",
    "fullName": "Nguyễn Văn A",
    "phone": "+84912345678",
    "email": "a@example.com",
    "customerSource": "web-client",
    "attributes": {
        "cap_bac": "VIP",
        "diem_tich_luy": 120
    }
}
headers = {
    "X-Api-Key": "omx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.json())
```

```javascript Tạo khách hàng mới
const url = 'https://api.omicx.one/customers';
const options = {
  method: 'POST',
  headers: {
    'X-Api-Key': 'omx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'Content-Type': 'application/json'
  },
  body: '{"status":"active","fullName":"Nguyễn Văn A","phone":"+84912345678","email":"a@example.com","customerSource":"web-client","attributes":{"cap_bac":"VIP","diem_tich_luy":120}}'
};

try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

```go Tạo khách hàng mới
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://api.omicx.one/customers"

	payload := strings.NewReader("{\n  \"status\": \"active\",\n  \"fullName\": \"Nguyễn Văn A\",\n  \"phone\": \"+84912345678\",\n  \"email\": \"a@example.com\",\n  \"customerSource\": \"web-client\",\n  \"attributes\": {\n    \"cap_bac\": \"VIP\",\n    \"diem_tich_luy\": 120\n  }\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("X-Api-Key", "omx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```ruby Tạo khách hàng mới
require 'uri'
require 'net/http'

url = URI("https://api.omicx.one/customers")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["X-Api-Key"] = 'omx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
request["Content-Type"] = 'application/json'
request.body = "{\n  \"status\": \"active\",\n  \"fullName\": \"Nguyễn Văn A\",\n  \"phone\": \"+84912345678\",\n  \"email\": \"a@example.com\",\n  \"customerSource\": \"web-client\",\n  \"attributes\": {\n    \"cap_bac\": \"VIP\",\n    \"diem_tich_luy\": 120\n  }\n}"

response = http.request(request)
puts response.read_body
```

```java Tạo khách hàng mới
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://api.omicx.one/customers")
  .header("X-Api-Key", "omx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
  .header("Content-Type", "application/json")
  .body("{\n  \"status\": \"active\",\n  \"fullName\": \"Nguyễn Văn A\",\n  \"phone\": \"+84912345678\",\n  \"email\": \"a@example.com\",\n  \"customerSource\": \"web-client\",\n  \"attributes\": {\n    \"cap_bac\": \"VIP\",\n    \"diem_tich_luy\": 120\n  }\n}")
  .asString();
```

```php Tạo khách hàng mới
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://api.omicx.one/customers', [
  'body' => '{
  "status": "active",
  "fullName": "Nguyễn Văn A",
  "phone": "+84912345678",
  "email": "a@example.com",
  "customerSource": "web-client",
  "attributes": {
    "cap_bac": "VIP",
    "diem_tich_luy": 120
  }
}',
  'headers' => [
    'Content-Type' => 'application/json',
    'X-Api-Key' => 'omx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  ],
]);

echo $response->getBody();
```

```csharp Tạo khách hàng mới
using RestSharp;

var client = new RestClient("https://api.omicx.one/customers");
var request = new RestRequest(Method.POST);
request.AddHeader("X-Api-Key", "omx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"status\": \"active\",\n  \"fullName\": \"Nguyễn Văn A\",\n  \"phone\": \"+84912345678\",\n  \"email\": \"a@example.com\",\n  \"customerSource\": \"web-client\",\n  \"attributes\": {\n    \"cap_bac\": \"VIP\",\n    \"diem_tich_luy\": 120\n  }\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift Tạo khách hàng mới
import Foundation

let headers = [
  "X-Api-Key": "omx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "Content-Type": "application/json"
]
let parameters = [
  "status": "active",
  "fullName": "Nguyễn Văn A",
  "phone": "+84912345678",
  "email": "a@example.com",
  "customerSource": "web-client",
  "attributes": [
    "cap_bac": "VIP",
    "diem_tich_luy": 120
  ]
] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://api.omicx.one/customers")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```