Addresses in Brazil
Which address fields are required, how CartaFlow fills in the rest from the postal code, and how to handle errors.
Addresses in the API follow the Brazilian postal format used by Correios, the national postal service. You only need to send three fields: the recipient’s name, the postal code and the building number. CartaFlow fills in the rest from the postal code.
How a Brazilian address is written
If you are new to Brazilian addresses, these are the parts, in the order they appear on an envelope:
Destinatário Exemplo <- name
Rua Exemplo, 128, Ap. 52 <- street, number, complement
Centro <- neighborhood (bairro)
01001-000 São Paulo/SP <- postal code (CEP), city, state (UF)- CEP is the postal code: 8 digits, usually written
00000-000. A CEP often identifies a single street, or one side of it, so it tells you the street, neighborhood, city and state. - Bairro is the neighborhood. Every urban address has one, and Correios uses it for sorting.
- UF is the two-letter code of the state, such as
SPfor São Paulo. - Number goes after the street name. Addresses without a number use
S/N("sem número"). - Complement is the apartment, block, suite or floor.
The address rule
- Required:
name,postal_codeandnumber. - Fields you send are used as you sent them. CartaFlow does not replace your neighborhood or street with the one in the postal code database.
- Fields you leave out are filled in from the postal code:
street,neighborhood,cityandstate. - If a field is missing and the postal code does not provide it, the API answers 422 with the code
address_incomplete, and nothing is created or charged. This happens with postal codes that cover a whole city, common in small towns, which have no street or neighborhood.
The same rule applies to a letter’s recipient and sender (to and from) and to the address of a verification.
The shortest address
"to": {
"name": "Destinatário Exemplo",
"postal_code": "01001-000",
"number": "128",
"complement": "Ap. 52"
}The letter comes back with the complete address, as it will be printed:
"to": {
"name": "Destinatário Exemplo",
"street": "Praça da Sé",
"number": "128",
"complement": "Ap. 52",
"neighborhood": "Sé",
"city": "São Paulo",
"state": "SP",
"postal_code": "01001-000"
}Part of the address from you, part from the postal code
If your records have the neighborhood as the customer wrote it, send it. CartaFlow uses your neighborhood and looks up only what is missing.
"to": {
"name": "Destinatário Exemplo",
"postal_code": "01001-000",
"number": "128",
"neighborhood": "Centro"
}"to": {
"name": "Destinatário Exemplo",
"street": "Praça da Sé",
"number": "128",
"complement": null,
"neighborhood": "Centro",
"city": "São Paulo",
"state": "SP",
"postal_code": "01001-000"
}The street, city and state came from the postal code. The neighborhood is the one you sent.
The complete address
When you already have every field, send them all. Nothing is looked up.
"to": {
"name": "Destinatário Exemplo",
"street": "Rua Exemplo",
"number": "128",
"complement": "Ap. 52",
"neighborhood": "Centro",
"city": "São Paulo",
"state": "SP",
"postal_code": "01001-000"
}Check before sending
POST /v1/letters/quote takes the same body as creating a letter and returns the complete address, without creating or charging anything. Use it to show your user the address before they confirm.
The fields
| Field | Required | Rules |
|---|---|---|
name | Yes | Name of the person or company, as it should appear on the envelope. Up to 60 characters. |
postal_code | Yes | CEP, 8 digits, with or without the hyphen: 01001-000 or 01001000. It must exist in the postal code database. Responses always return it as 00000-000. |
number | Yes | Building number, as a string, because some include letters (128A). Use S/N when the address has no number. Up to 10 characters. |
complement | No | Apartment, block, suite, floor. Up to 60 characters. Not filled in from the postal code: printed only if you send it. |
street | No | Street type and name, without the number: Rua Exemplo. Up to 100 characters. Filled in from the postal code when you leave it out. |
neighborhood | No | Neighborhood (bairro). Up to 60 characters. Filled in from the postal code when you leave it out. |
city | No | City. Up to 60 characters. Filled in from the postal code when you leave it out. |
state | No | UF: the two-letter state code, in capitals. Filled in from the postal code when you leave it out. |
In responses every field is always present. complement is null when the address has none.
State codes (UF)
ACAcreALAlagoasAPAmapáAMAmazonasBABahiaCECearáDFDistrito FederalESEspírito SantoGOGoiásMAMaranhãoMTMato GrossoMSMato Grosso do SulMGMinas GeraisPAParáPBParaíbaPRParanáPEPernambucoPIPiauíRJRio de JaneiroRNRio Grande do NorteRSRio Grande do SulRORondôniaRRRoraimaSCSanta CatarinaSPSão PauloSESergipeTOTocantinsFill in your customer’s form from the postal code
GET /v1/postal-codes/{postal_code} returns the street, neighborhood, city and state of a postal code. It is the same lookup the API uses to complete addresses. Free of charge.
curl "https://api.cartaflow.com.br/v1/postal-codes/01001-000" \
-H "Authorization: Bearer $CARTAFLOW_API_KEY"{
"object": "postal_code",
"postal_code": "01001-000",
"street": "Praça da Sé",
"neighborhood": "Sé",
"city": "São Paulo",
"state": "SP"
}street and neighborhood are null when the postal code covers a whole city. In that case, ask your customer for the street and neighborhood and send both with the letter.
Call the API from your server
The API key never goes to the browser or to an app. Your form calls your server, and your server calls CartaFlow. See Authentication.
Sender
The sender is printed on one line above the recipient. Correios returns the letter to this address when it cannot be delivered.
- Send
fromwith the letter to use a specific sender, with the same field rules as the recipient. - If you leave out
from, we use the account’s default sender, which you set in the dashboard under Configurações > Empresa (Settings > Company). It shows asdefault_senderin GET /v1/account.
Addresses in Brazil only
CartaFlow prints and posts in Brazil, to addresses in Brazil. That is why there is no country field and the postal code is always required.
Address errors
Address problems come back as a 422 validation_error, with one item in details per field. param names the field, for example to.postal_code or from.street.
details[].code | What to do |
|---|---|
postal_code_not_found | The postal code is not in the database. Check the 8 digits with your customer. |
address_incomplete | The postal code covers a whole city and does not provide the field in param. Send that field, usually street and neighborhood. |
invalid_format | The postal code does not have 8 digits. Remove letters and spaces. |
too_long | The field is longer than allowed. Abbreviate, for example Avenida to Av. and Apartamento to Ap., as Brazilians do on envelopes. |
required | name, postal_code or number is missing. |
{
"error": {
"type": "validation_error",
"code": "invalid_parameters",
"message": "One field has a problem. See details.",
"request_id": "req_9Vb4Xk2Lq8Mz",
"doc_url": "https://docs.cartaflow.com.br/en/errors#invalid_parameters",
"details": [
{
"param": "to.street",
"code": "address_incomplete",
"message": "Postal code 13165-000 covers the whole city and has no street. Send street and neighborhood."
}
]
}
}The full list of codes is in Errors.