Building Agents on Envia

Endpoints for AI agents: quote every carrier in one call, and turn text, a photo or a voice note into an address.

Everything here can be done with the regular API. This surface exists because a language model calling an API needs a different shape from a developer writing an integration — and because a buyer sending you an address on WhatsApp is not sending you JSON.

If you are writing a normal integration, use the Shipping API. If you are building an assistant, a chatbot, or anything where a model decides what to call, start here.

What is actually different

Regular APIThis surface
FieldsNested, camelCaseFlat, snake_case — a model fills these reliably, nested objects it does not
QuotingOne carrier per requestEvery carrier in one call, cheapest already chosen
A failing carrierFails the callComes back in its own entry with its error; the rest still answer
AddressesYou send structured fieldsSend the text or the photo; we structure it

One call, every carrier, cheapest chosen

API reference → Quote every carrier at once

This is the difference worth knowing even if you never build an agent.

curl --request POST \
  --url "https://queries.test.envia.com/ai/shipping/rate" \
  --header "Authorization: Bearer $ENVIA_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
    "origin_zip": "64000",
    "destination_zip": "06600",
    "weight": 1,
    "length": 20,
    "width": 15,
    "height": 10,
    "content": "Documentacion"
  }'
{
  "carriers_considered": ["afimex", "amPm", "coordinadora", "dhl", "fedex", "…"],
  "results": [
    { "carrier": "coordinadora", "ok": true, "data": { "meta": "rate", "data": [ … ] } },
    { "carrier": "afimex", "ok": true, "data": { "meta": "error",
        "error": { "code": 1300, "message": "Coverage not available" } } }
  ],
  "cheapest": {
    "carrier": "paquetexpress", "service": "ground_od",
    "totalPrice": 11.6, "currency": "MXN"
  },
  "cheapestByCurrency": { "MXN": { "…": "…" } }
}

Three things to notice:

cheapest is already worked out. With POST /ship/rate/ you call once per carrier and compare the results yourself. Here you do not.

A carrier that fails does not fail the call. It comes back with ok: true and an error inside data — the carrier answered, and the answer was "no coverage". Read data.meta to tell a price from a refusal.

carriers_considered tells you who was asked. If a carrier you expected is missing from it, the problem is your account's carrier configuration, not the quote.

Sending carriers: ["fedex", "dhl"] narrows the fan-out when you already know who you want.

When someone is waiting, stream it

A fan-out is as slow as its slowest carrier. POST /ai/shipping/rate-stream takes the same body and pushes each result over Server-Sent Events as it arrives, so the first prices appear in a fraction of the total.

It returns text/event-stream. Read it with an SSE client — response.json() will not work.

From what the buyer actually sent you

API reference → Turn free text or a photo into an address

In much of Latin America an address arrives as a WhatsApp message or a screenshot, not as seven form fields.

curl --request POST \
  --url "https://queries.test.envia.com/ai/shipping/parse-address" \
  --header "Authorization: Bearer $ENVIA_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
    "text": "Mandamelo a Ana Lopez, Av. Constitucion 1500, colonia Centro, Monterrey Nuevo Leon, CP 64000, su tel es 8112345678",
    "country": "MX"
  }'

You get back more than you sent:

{
  "success": true,
  "data": {
    "name": "Ana Lopez", "street": "Av. Constitucion 1500",
    "district": "Centro", "city": "Monterrey",
    "state": "NL",
    "postal_code": "64000",
    "phone": "8112345678", "phone_code": "52",
    "coordinates": { "latitude": "25.671795", "longitude": "-100.301101" },
    "suburbs": ["Centro", "Buenos Aires", "Contry", "…"]
  }
}

state comes back as the ISO code, not as the buyer wrote it — which is what the rest of the API expects. suburbs is the list of valid neighbourhoods for that postal code, so you can offer a picker instead of a free-text box, and stop the single most common cause of a returned parcel.

Send an image instead by passing image as base64 with its mimeType. Up to 7 MB.

Treat the result as a draft, not as truth. It is good and it is not infallible — an interior number can land in the wrong field. Show it to the person and let them confirm before you buy a label with it.

Voice notes

POST /ai/shipping/transcribe-audio takes an audio file as multipart/form-data, up to 10 MB, and returns the text. Feed that text to parse-address and a voice note becomes a shipment.

Addresses differ by country

GET /ai/shipping/address-requirements/{country} returns which fields that country needs and what they are called there. Ask before you render a form — a Mexican address form shown to a buyer in Brazil will collect the wrong things. See Country address rules for the underlying differences.

Remembering the conversation

An assistant that forgets the previous message is not much of an assistant. Conversations store the thread:

  • You choose the id — a UUID. Sending it again replaces what was there, so save the whole message list each time
  • channel records where it happened: web, whatsapp, facebook, instagram, telegram, line, viber, twitter
  • external_contact_id ties the thread to that channel's own id for the person — the WhatsApp number, say — so you can pick it up again when they come back

GET /ai/conversations/usage reports your daily and monthly counts.

What this surface does not change

It is the same account, the same token, the same balance and the same carriers. ai/shipping/generate creates a real label and charges for it exactly as POST /ship/generate/ does.

The errors are the Management API's, not the Shipping API's — status codes, not meta: "error" — except inside results[].data, which carries the Shipping API's own shape untouched. See Errors.

When an agent has to file a claim

Claims are the one place where an agent can confidently do the wrong thing: open the wrong type, outside the window, without the documents. The catalog answers all three — GET /tickets/types returns, per claim type, an mcp_context written for exactly this:

"mcp_context": {
  "use_case": "Carrier charged extra weight on a shipment. Use ONLY for weight surcharges, NOT for movement or delay issues.",
  "is_blocked": false,
  "requires_guide": true,
  "agent_notes": ["Use ONLY for weight surcharges. Do NOT use for tracking movement or delay issues."]
}

Alongside it, conditions carries the shipment statuses the claim is allowed from and any time window, and files the documents it needs. Feed those into the agent's context instead of hardcoding rules: is_blocked: true means the agent must not open that type at all. See Tickets Overview for the full shape, and Incidences for the queue of shipments that usually need one.

Related pages


Next steps


Did this page help you?