# Sending EPCIS 2.0 events to CropPath

A partner system (an ERP, a packing house's line system, a buyer's or importer's EPCIS repository) can send the
supply-chain events it records for a tenant's lots, cartons and shipments as a GS1 EPCIS 2.0 JSON-LD document. CropPath
checks each event, links it to the tenant's records and answers with one result per event. This page covers what to
send, how events map, the limits, and what a captured event can and cannot prove.
A complete example is [`epcis-capture-example.jsonld`](epcis-capture-example.jsonld), served at
`/developers/downloads/epcis-capture-example.jsonld`.

## Access

`POST /v1/epcis/capture` with `Authorization: Bearer agt_...`, an API token of the tenant with the role
`tenant_admin`. A tenant admin creates it in the admin console (Settings, API tokens), gives it a name that says which
partner holds it, and revokes it when the partnership ends. One token per partner system keeps the captures apart:
each captured event records the token as `token-<id>`, never its name. An operator or auditor token is refused (403);
every event goes to the token's own tenant only.

The body is the document, `Content-Type: application/ld+json` (or `application/json`), at most 1 MiB.

```bash
curl -sSO https://<host>/developers/downloads/epcis-capture-example.jsonld
curl -sS https://<host>/v1/epcis/capture \
  -H "Authorization: Bearer $AGRITRACE_TOKEN" \
  -H "Content-Type: application/ld+json" \
  --data-binary @epcis-capture-example.jsonld
```

## The document

An `EPCISDocument` (EPCIS 2.0, JSON-LD binding) whose `@context` names
`https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld` (or the unversioned
`https://ref.gs1.org/standards/epcis/epcis-context.jsonld`), `schemaVersion` `2.0` (any 2.x), an RFC 3339
`creationDate`, and at most 500 events in `epcisBody.eventList`. Extension members are allowed anywhere the EPCIS
schema allows them, as compact IRIs of a prefix your context declares (`erp:harvestCrew`) or as absolute IRIs. A
document that is not this is refused as a whole: 400 `invalid_document` (or `invalid_json`), 413 `too_many_events`
or `body_too_large`, and nothing is written.

Each event is checked against the rules of the GS1 EPCIS 2.0.0 JSON schema for the members CropPath reads. On top of the schema, CropPath requires:

| Member | Requirement |
|---|---|
| `type` | `ObjectEvent`, `AggregationEvent` or `TransformationEvent`. |
| `eventID` | Required, a URI (a `urn:uuid:` is usual). It identifies the event for good: see "Retries". |
| `eventTime`, `eventTimeZoneOffset` | RFC 3339 with an offset, and `+hh:mm`/`-hh:mm`. |
| `action` | `ADD` or `OBSERVE` (ObjectEvent, AggregationEvent). `DELETE` is not taken. |
| `bizStep` | One of the bare CBV 2.0 terms below, or its web vocabulary form `https://ref.gs1.org/cbv/BizStep-<term>`. |
| `errorDeclaration` | Not taken: a correction is not applied to captured events. |

| `bizStep` | Recorded as |
|---|---|
| `commissioning` | the harvest |
| `receiving` | a receipt |
| `packing` | packing |
| `shipping` | shipping |
| `sensor_reporting` | an observation (`observe`): your sensor data about the lot, carton or shipment |

### Sensor data (`sensor_reporting`)

A `sensor_reporting` event carries readings your loggers or stations made about a lot, carton or shipment the event
names (in `quantityList` or `epcList`), for example a cold room's temperature while the lot was stored. It must be an
`ObjectEvent` with `action` `OBSERVE` and a `sensorElementList` of at most 20 sensor elements and 200 sensor reports
in all. Each sensor element is checked member by member against the GS1 EPCIS 2.0 JSON schema's `sensorElement`,
`sensorMetadata` and `sensorReport`: known members only (or extension members as IRIs), date-times with an offset,
URIs where the schema has URIs, numbers for `value`, `minValue`, `maxValue`, `meanValue`, `sDev`, `percRank` and
`percValue`, a `type` that is a GS1 measurement type the schema lists (`Temperature`, `RelativeHumidity`,
`VolumeFraction` and the others) or a URI of your own vocabulary (not a `https://gs1.org/voc/` URI), a `component`
and an `exception` from the schema's lists or a URI, and at least one report, each with a `type`. Use the UN/ECE
Recommendation 20 code as `uom` (`CEL`, `P1`), as EPCIS does.

A `sensor_reporting` event that names none of your lots, cartons or shipments (only a read point, such as the daily
summary of a growing area that CropPath's own export version 2 or 3 writes) is `no_reference`: CropPath records sensor
data about a growing area only from devices that send their signed envelopes themselves.

## Identifiers

Every lot, carton and shipment an event names must be a record the tenant already holds; a capture never creates one.

| Record | Where in the event | Identifier |
|---|---|---|
| Lot | an EPC class: `quantityList`, `childQuantityList`, `inputQuantityList`, `outputQuantityList` | `urn:agritrace:lot:<lot code>`, or `https://id.gs1.org/01/<GTIN>/10/<lot code>` when the lot carries that GTIN |
| Carton | an EPC: `epcList`, `parentID`, `childEPCs`, `inputEPCList`, `outputEPCList` | `urn:agritrace:carton:<carton code>` |
| Shipment | an EPC, or a `bizTransactionList` entry | `urn:agritrace:shipment:<shipment code>` |

Codes are the ones CropPath shows (lot `LO-260928-014`, carton `C-001-0001`, shipment `SH-260930-01`). Other business
transactions (an invoice, a purchase order) are kept with the event and link nothing. `readPoint` and `bizLocation`
are kept as sent; CropPath does not resolve a partner's locations. CropPath's own lot export (`GET /v1/lots/{lot}/epcis`)
uses the lot form above, so a document exported by one CropPath tenant can be captured by another that holds a lot of
the same code.

## The report

`200` with one result per event, in document order (`index` is the event's position):

```json
{
  "capture_id": 12, "events": 3, "accepted": 2, "duplicates": 0, "rejected": 1,
  "source": "partner_capture", "tier": "self_declared",
  "results": [
    {"index": 0, "event_id": "urn:uuid:...-000000000001", "status": "accepted", "step": "harvest",
     "references": [{"kind": "lot", "code": "LO-260928-014"}]},
    {"index": 1, "event_id": "urn:uuid:...-000000000002", "status": "duplicate"},
    {"index": 2, "event_id": "urn:uuid:...-000000000003", "status": "rejected", "code": "unknown_reference",
     "detail": "urn:agritrace:carton:C-999-0001"}
  ]
}
```

| `code` | Meaning |
|---|---|
| `invalid_event` | Not what the EPCIS schema allows (`detail` names the member). |
| `unsupported_event_type` | AssociationEvent, TransactionEvent or an extension event type. |
| `missing_event_id` | No `eventID`. |
| `unsupported_action` | `DELETE`. |
| `unsupported_error_declaration` | The event carries an `errorDeclaration`. |
| `unsupported_biz_step` | No `bizStep`, or one outside the five above, or `sensor_reporting` on anything but an `ObjectEvent` with `action` `OBSERVE` and a `sensorElementList`. |
| `event_too_large` | Over 32 KiB as canonical JSON, or a `sensor_reporting` event with more than 20 sensor elements or 200 sensor reports. |
| `event_too_deep` | Objects and arrays nested more than 32 levels (the event itself is level 1). |
| `event_id_conflict` | The tenant already holds this `eventID` with different content. |
| `no_reference` | The event names no lot, carton or shipment. |
| `unknown_reference` | It names an identifier that is not a lot, carton or shipment of the tenant (`detail` quotes it). |

A rejected event writes nothing; the other events of the document are still taken.

## Retries

Send the same document again after a timeout: an event whose `eventID` the tenant already holds with the same content
(compared on its canonical JSON, RFC 8785) is reported `duplicate` and not stored twice. The same `eventID` with other
content is `event_id_conflict`: an EPCIS event is never edited, so send a new event with a new `eventID` instead.

## Limits

At most 1 MiB per request, 500 events per document, 32 KiB, 200 identifiers and 32 levels of nesting per event, and
20 sensor elements and 200 sensor reports per `sensor_reporting` event.
Captures of one tenant are taken one at a time. Each request writes one audit row (`epcis.capture`, with the capture
id and the counts, never the events).

## What a captured event proves (trust tiers)

A captured event is the partner's statement. CropPath stores it with `source` `partner_capture` and `tier`
`self_declared` and does not verify it: the trust tiers of the lot and its
fruits, the seal and the public pages never read captured events, so a capture cannot raise any tier. A signature an
event may carry (CropPath's own export includes the device's) is kept as sent and not checked.

A captured event is therefore Self-declared even when it carries a valid device signature. Captured sensor data is
Self-declared too, whatever quality flag, calibration or location verdict its reports state: it is kept with the
event, and never becomes one of CropPath's sensor readings, their daily summaries or evidence for a trust tier. Evidence counts above
Self-declared only when the device sends its signed envelope itself to `POST /v1/ingest/{route}`, where CropPath checks
the signature and that a CropPath platform admin approved the device, its key and, when a gateway's key signed it, the
gateway (ADR 0001, #201). The same holds for the batch routes (`POST /v1/harvests/batch` and the others): a batch is
the company's own statement, a batch-declared harvest is stored `self_declared`, and a batch item cannot carry a
signature (a body or item with a member its route does not define is refused with 400 and nothing is written).

## Reading captured events

`GET /v1/lots/{lot}/captured-events?limit=50&cursor=...` lists the events captured for a lot: those naming the lot or
one of its cartons, and those naming the shipment that carries it, oldest capture first. Each item gives the step,
the times and places as sent, the references, who captured it and the `self_declared` tier. It is read like the lot
(admins and auditors of the tenant, an operator within its facilities) and is not audited. An operator scoped to
some facilities sees only the events whose every lot, carton and shipment is in those facilities: an event that also
names a record of another facility is left out whole, whether it was found through the lot or through its shipment.
