Docs

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 SP for 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_code and number.
  • 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, city and state.
  • 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.

Request
"to": {
  "name": "Destinatário Exemplo",
  "postal_code": "01001-000",
  "number": "128",
  "neighborhood": "Centro"
}
Address in the response
"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

FieldRequiredRules
nameYesName of the person or company, as it should appear on the envelope. Up to 60 characters.
postal_codeYesCEP, 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.
numberYesBuilding number, as a string, because some include letters (128A). Use S/N when the address has no number. Up to 10 characters.
complementNoApartment, block, suite, floor. Up to 60 characters. Not filled in from the postal code: printed only if you send it.
streetNoStreet type and name, without the number: Rua Exemplo. Up to 100 characters. Filled in from the postal code when you leave it out.
neighborhoodNoNeighborhood (bairro). Up to 60 characters. Filled in from the postal code when you leave it out.
cityNoCity. Up to 60 characters. Filled in from the postal code when you leave it out.
stateNoUF: 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)

ACAcre
ALAlagoas
APAmapá
AMAmazonas
BABahia
CECeará
DFDistrito Federal
ESEspírito Santo
GOGoiás
MAMaranhão
MTMato Grosso
MSMato Grosso do Sul
MGMinas Gerais
PAPará
PBParaíba
PRParaná
PEPernambuco
PIPiauí
RJRio de Janeiro
RNRio Grande do Norte
RSRio Grande do Sul
RORondônia
RRRoraima
SCSanta Catarina
SPSão Paulo
SESergipe
TOTocantins

Fill 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"
Response
{
  "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 from with 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 as default_sender in 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[].codeWhat to do
postal_code_not_foundThe postal code is not in the database. Check the 8 digits with your customer.
address_incompleteThe postal code covers a whole city and does not provide the field in param. Send that field, usually street and neighborhood.
invalid_formatThe postal code does not have 8 digits. Remove letters and spaces.
too_longThe field is longer than allowed. Abbreviate, for example Avenida to Av. and Apartamento to Ap., as Brazilians do on envelopes.
requiredname, postal_code or number is missing.
Example 422 response
{
  "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.

On this page