Documentation Index
Fetch the complete documentation index at: https://docs.commodityai.io/llms.txt
Use this file to discover all available pages before exploring further.
Creates a purchase or sale contract and returns the public contract shape with generated CommodityAI identifiers and references.
Create Contract is the entry point for standard object flows around purchasing, sales, quality, pricing, and allocation. The public request should stay focused on commercial contract data. Advanced structures such as formula pricing and extension fields are available when they have been enabled for your integration.
| Header | Required | Notes |
|---|
Idempotency-Key | Yes | Unique key for safely retrying this write. Use a stable value from your source system, such as erp-contract-10024-create. |
Core Contract Fields
Contract side. Values: purchase, sale.
Counterparty UUID for the supplier or buyer.
Stable source-system contract reference. Use this for cross-system reconciliation, not as a replacement for Idempotency-Key.
Contract date in YYYY-MM-DD format.
Product catalog UUID. Prefer this when using CommodityAI product reference data.
Product name when not using a product catalog ID.
Contract quantity as a positive numeric string.
Pricing type. Values: fixed, futures, formula.
Fixed price as a positive numeric string.
Currency UUID for the quoted price.
Unit UUID for the quoted price.
Incoterms value such as FOB, CIF, or CFR.
Shipment period start date in YYYY-MM-DD format.
Shipment period end date in YYYY-MM-DD format.
Payment terms type. Values: net, prepayment, cad, lc, open_account.
Number of days for net-style payment terms.
standard_payment_terms_id
Standard payment term UUID from reference data.
External References
Use external_reference for the stable identifier from your ERP, CTRM, or integration source. It should not change when a contract is amended or retried.
Use Idempotency-Key for retry protection. Unless CommodityAI has configured duplicate prevention for your integration, do not assume external_reference alone makes create requests idempotent.
Optional Structures
Optional contract legs. Use legs when a contract needs tranche-level quantity, shipment windows, delivery terms, or other overrides.
Structured product attribute values keyed by product attribute UUID. For select attributes, the value is an option UUID. For multi_select attributes, the value is an array of option UUIDs.
Optional contract-scoped quality specifications. Requires product_id.
If legs is omitted, CommodityAI creates one default leg with label 0 that inherits the contract fields. If legs is provided, it must contain at least two legs. Labels default to A, B, C, and must be unique uppercase letters.
Each leg has:
| Field | Type | Notes |
|---|
label | string | Optional single uppercase letter. Defaults to A, B, C, … in tranched mode. |
quantity | string | Positive numeric string. |
overrides | object | Optional overrides for inherited contract fields. |
Advanced Structures
Formula pricing terms for the contract or a specific leg. See Pricing Terms.
Company-specific contract extension fields enabled for your integration.
Side Effects
Creating a contract can also create or schedule related standard-object work:
- A default leg or lettered contract legs are created for allocation and fulfillment.
- Contract quality specifications may be seeded from product or grade templates.
- Sale contracts may create expected letter-of-credit workflow records when LC terms apply.
- Document requirements may be synced and requirement checks may be scheduled.
Request Example
curl -X POST "https://commodityai.app/api/v1/contracts" \
-H "Authorization: Bearer cai_live_your_api_key_here" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: erp-contract-10024-create" \
-d '{
"type": "purchase",
"counterparty_id": "223e4567-e89b-12d3-a456-426614174001",
"external_reference": "ERP-PO-10024",
"contract_date": "2026-05-19",
"product_id": "923e4567-e89b-12d3-a456-426614174009",
"grade_id": "a23e4567-e89b-12d3-a456-426614174010",
"quantity": "50000",
"quantity_unit_id": "323e4567-e89b-12d3-a456-426614174002",
"pricing_type": "fixed",
"price": "12.50",
"price_currency_id": "d23e4567-e89b-12d3-a456-426614174013",
"price_unit_id": "323e4567-e89b-12d3-a456-426614174002",
"incoterms": "FOB",
"shipment_start": "2026-06-01",
"shipment_end": "2026-06-30",
"payment_terms_type": "net",
"payment_terms_days": 30,
"quality_attributes": {
"b23e4567-e89b-12d3-a456-426614174011": "Brazil",
"c23e4567-e89b-12d3-a456-426614174012": "34"
},
"legs": [
{
"label": "A",
"quantity": "25000",
"overrides": {
"shipment_start": "2026-06-01",
"shipment_end": "2026-06-15"
}
},
{
"label": "B",
"quantity": "25000",
"overrides": {
"shipment_start": "2026-06-16",
"shipment_end": "2026-06-30"
}
}
]
}'
Public Response Shape
The response is a public API shape, not a raw internal database object. Stable fields include:
| Field | Notes |
|---|
contract.id | CommodityAI contract UUID. |
contract.contract_number | Generated display reference such as P0001 or S0001. |
contract.type | purchase or sale. |
contract.status | Current lifecycle status. New contracts usually start as draft. |
contract.external_reference | Source-system reference when provided. |
legs[] | Public leg IDs, labels, quantities, and generated leg references when available. |
warnings[] | Optional non-blocking warnings from downstream workflow setup. |
{
"contract": {
"id": "123e4567-e89b-12d3-a456-426614174000",
"contract_number": "P0001",
"type": "purchase",
"status": "draft",
"external_reference": "ERP-PO-10024"
},
"legs": [
{
"id": "423e4567-e89b-12d3-a456-426614174003",
"label": "A",
"quantity": "25000",
"reference": "P0001.A"
},
{
"id": "523e4567-e89b-12d3-a456-426614174004",
"label": "B",
"quantity": "25000",
"reference": "P0001.B"
}
],
"warnings": []
}
Errors and Warnings
Errors use stable HTTP status codes and machine-readable error_code values where a business rule blocks the request.
| Status | Code | Meaning |
|---|
400 | validation_error | Required fields, IDs, date formats, product attribute values, or pricing payloads are invalid. |
404 | not_found | A referenced counterparty, product, grade, unit, currency, port, or template was not found for the company. |
409 | duplicate_or_conflict | The request conflicts with an existing contract or integration rule. |
422 | business_rule_failed | The contract is structurally valid but fails a CommodityAI contract rule. |
Warnings are returned in warnings[] when CommodityAI accepts the contract but needs to report non-blocking workflow setup issues.