# CNPJ coordinates and maps in the API

> Get the latitude and longitude of a CNPJ's address via the API, with the point's precision, and render map, satellite and street view images.

> **Tip:** Concepts on this page: [Address coordinates and imagery](https://cnpja.com/en-us/docs/data/location) and [Resources with their own rules](https://cnpja.com/en-us/docs/data/freshness#special-rules).

The API turns a CNPJ's registered address into three forms of location: the geographic coordinates, a map image and a street-level image. The coordinates are an optional resource of [Lookup CNPJ](https://cnpja.com/en-us/docs/api/reference#tag/establishments/GET/v2/establishments/%7btaxId%7d), requested in `expand`, and the images have their own operations. All of them are generated from the registered address by a third-party service, described in [Data sources](https://cnpja.com/en-us/docs/data/sources#google-maps), and require an [API key](https://cnpja.com/en-us/docs/api/first-request).

The location is always that of the CNPJ looked up, based on the [establishment](https://cnpja.com/en-us/docs/glossary#estabelecimento--establishment)'s address in the Receita Federal registry, and the images carry no coordinates. Where the coordinates come from, their precision and the case of an address that cannot be located are in [Address coordinates and imagery](https://cnpja.com/en-us/docs/data/location).

## Request

| Operation | Method and path | Response |
|---|---|---|
| Lookup CNPJ | `GET /v2/establishments/{taxId}?expand=address.coordinates` | Registration JSON, with `address.coordinates` |
| Render CNPJ map | `GET /v2/establishments/{taxId}/map` | PNG image |
| Render CNPJ street view | `GET /v2/establishments/{taxId}/street-view` | JPEG image |

In all three operations, `taxId` is the CNPJ with 14 characters, no punctuation, uppercase letters in an [alphanumeric CNPJ](https://cnpja.com/en-us/docs/glossary#cnpj-alfanumérico--alphanumeric-cnpj) and valid check digits.

### Lookup CNPJ

Add `address.coordinates` to `expand` on Lookup CNPJ. The value can be combined with the other optional resources, as in `expand=address.coordinates,company.taxRegime`, described in [CNPJ lookup](https://cnpja.com/en-us/docs/api/cnpj-lookup).

| Parameter | Location | Use |
|---|---|---|
| `taxId` | Path | Establishment CNPJ |
| `expand` | Query | Include `address.coordinates` to receive the coordinates |

```http
GET /v2/establishments/37335118000180?expand=address.coordinates HTTP/1.1
Host: api.cnpja.com
Authorization: YOUR_API_KEY
```

`maxAge` still applies to the registration data, but not to the coordinates, as explained in [Sources and updates](#sources-and-updates). There is no `address.coordinates.maxAge` parameter.

### Render CNPJ map

Render CNPJ map returns a map centered on the registered address, with a marker at that point.

| Parameter | Location | Use |
|---|---|---|
| `taxId` | Path | Establishment CNPJ |
| `type` | Query | Map style, described in [Image options](#image-options). Defaults to `ROADMAP` |

```bash
curl "https://api.cnpja.com/v2/establishments/37335118000180/map" \
  -H "Authorization: YOUR_API_KEY" \
  -o map.png
```

### Render CNPJ street view

Render CNPJ street view returns an image of the registered address seen from street level. The operation has no query parameters: only the CNPJ in the path.

| Parameter | Location | Use |
|---|---|---|
| `taxId` | Path | Establishment CNPJ |

```bash
curl "https://api.cnpja.com/v2/establishments/37335118000180/street-view" \
  -H "Authorization: YOUR_API_KEY" \
  -o street-view.jpg
```

## Response fields

With `expand=address.coordinates`, the coordinates come inside `address`. Response trimmed to the address:

```json
{
  "taxId": "37335118000180",
  "address": {
    "street": "Avenida Brigadeiro Faria Lima",
    "number": "2369",
    "district": "Jardim Paulistano",
    "city": "São Paulo",
    "state": "SP",
    "coordinates": {
      "latitude": -23.5774497,
      "longitude": -46.6864131,
      "precision": "ADDRESS"
    }
  }
}
```

| Field | Content |
|---|---|
| `address.coordinates` | Coordinates of the registered address. Absent when `address.coordinates` is not in `expand` |
| `address.coordinates.latitude` | Latitude in decimal degrees, negative south of the Equator, or `null` when the address cannot be located |
| `address.coordinates.longitude` | Longitude in decimal degrees, negative west of the Greenwich meridian, or `null` when the address cannot be located |
| `address.coordinates.precision` | Point the coordinates stand for: `ADDRESS` (the address itself), `DISTRICT` (the district) or `MUNICIPALITY` (the municipality seat), described in [Precision](https://cnpja.com/en-us/docs/data/location#precision), or `null` when the address cannot be located |

- Handle the `null` case before placing the establishment on a map or calculating distances.
- Check that `precision` is `ADDRESS` before treating the point as the establishment's location or rendering the map and the street view, which at `DISTRICT` or `MUNICIPALITY` would not show the establishment.
- The other `address` fields follow the format described in [CNPJ lookup](https://cnpja.com/en-us/docs/api/cnpj-lookup).

### Images

A `200` response from the image operations carries the file itself, not JSON. Size and framing are in [Fixed characteristics](#fixed-characteristics).

| Operation | `Content-Type` | Format |
|---|---|---|
| Render CNPJ map | `image/png` | PNG image of 640 × 640 pixels |
| Render CNPJ street view | `image/jpeg` | JPEG image of 640 × 640 pixels |

Errors come as JSON, in the same format as every other operation. Check the status before using the file:

```js
const response = await fetch('https://api.cnpja.com/v2/establishments/37335118000180/map', {
  headers: { Authorization: process.env.CNPJA_API_KEY },
});

if (response.status === 200) {
  const image = Buffer.from(await response.arrayBuffer());
  // use the image within the image usage rules
} else {
  const error = await response.json();
}
```

Render the images on your server, where the key is kept, and never in the browser, as explained in [Authentication](https://cnpja.com/en-us/docs/api/authentication). If you store the file to show it again, respect the storage limits in the Google Maps Platform terms, as the [Terms of Use](https://cnpja.com/en-us/legal/terms#data) state, and the [image usage rules](https://cnpja.com/en-us/docs/data/location#usage-rules).

The images are rendered from the registered address, sent as text to the maps service, without checking the coordinates or their precision. That is why the image operations do not answer `404` for a missing location or street view:

- When the service returns an image, the response is `200`, even when it does not show the establishment: the map is centered where the service places the address, and a place with no street imagery gets a gray image, with no photo, in place of the street view. That image is charged like any other.
- To avoid rendering images of a point that is not the establishment's, check first that `address.coordinates.precision` is `ADDRESS`. Even at that precision, the street view can come back gray.
- A failure of the maps service ends in a `500`, which is not charged, as the [metering rules](https://cnpja.com/en-us/legal/metering#not-charged) set. Retry the request later.

## Image options

### Map type

The `type` parameter of Render CNPJ map picks the style of the image:

| `type` | Image |
|---|---|
| `ROADMAP` | Street and road map (default) |
| `TERRAIN` | Map with terrain elevation |
| `SATELLITE` | Satellite imagery |
| `HYBRID` | Satellite imagery with streets and roads overlaid |

```http
GET /v2/establishments/37335118000180/map?type=SATELLITE HTTP/1.1
Host: api.cnpja.com
Authorization: YOUR_API_KEY
```

A value outside this list is rejected with `400` `REQUEST_VALIDATION_FAILED`.

### Fixed characteristics

The other characteristics of the images cannot be configured:

| Characteristic | Map | Street view |
|---|---|---|
| Size | 640 × 640 pixels | 640 × 640 pixels |
| Framing | Centered on the address, with a marker, at zoom level 17 | 90° horizontal field of view, with no choice of heading or pitch |
| Extra parameters | Rejected with `400` `REQUEST_VALIDATION_FAILED`, such as `zoom` | Ignored |

To show a larger area around the address, use the coordinates on your own map, where you control the zoom.

## Sources and updates

The coordinates are calculated once per address and shared by the establishments with the same registered address, and the images are generated on every request, as explained in [Address coordinates and imagery](https://cnpja.com/en-us/docs/data/location). In the API, this works as follows:

- **Coordinates do not follow `maxAge`.** A smaller `maxAge` can refresh the address, but it does not recompute the coordinates or the precision of an address that did not change.
- **Address not located.** When the address cannot be located, `latitude`, `longitude` and `precision` are `null`, and the computation is repeated on every request that includes `address.coordinates`. Coordinates with `DISTRICT` or `MUNICIPALITY` precision already count as located and are not recomputed on every request.
- **Images use the cached address.** Map and street view accept no `maxAge` and start from the address already stored, which Lookup CNPJ updates. To render the image of a recently changed address, first look up the CNPJ with a short `maxAge`.
- **CNPJ not in cache.** A CNPJ that is not in cached data yet is queried live at the Receita Federal before the image is generated.

The general rules for cache and `maxAge` are in [Max age and partial success](https://cnpja.com/en-us/docs/api/max-age).

### Partial success

Computing the coordinates is a live query, with up to 30 seconds. If it fails or exceeds that time, Lookup CNPJ responds with `206`, with `latitude`, `longitude` and `precision` set to `null`, and delivers the other resources normally. The coordinates are not charged in that case; repeat the request later. See [Max age and partial success](https://cnpja.com/en-us/docs/api/max-age).

The image operations never return `206`: the image is generated in full or the request fails.

## Billing

`address.coordinates` is charged as a Lookup CNPJ resource, cached when the coordinates are already computed and live when they are computed during the request, and each image generated is charged live, with no reuse and with no separate charge for the Receita Federal query of a CNPJ not in cache. The consumption of each case is in the Metering Rules, under [CNPJ lookup](https://cnpja.com/en-us/legal/metering#api-cnpj) and [Generate map and street view](https://cnpja.com/en-us/legal/metering#api).

> **Important:** request `address.coordinates` only when you will use the coordinates. While the address cannot be located, every request with this value repeats the computation and is charged as a [live query](https://cnpja.com/en-us/docs/glossary#consulta-em-tempo-real--live-query), as explained in [Address not located](https://cnpja.com/en-us/docs/data/location#address-not-located).

## Frequent errors

| Status | Code | Cause | Action |
|---|---|---|---|
| `400` | `REQUEST_VALIDATION_FAILED` | CNPJ not in the 14-character format or with invalid check digits | Send the CNPJ with no punctuation and with uppercase letters |
| `400` | `REQUEST_VALIDATION_FAILED` | On Render CNPJ map, `type` outside the accepted values or an unrecognized parameter, such as `zoom` | Send a value from [Map type](#map-type) and check the spelling of the parameters, since `errors` does not name the rejected one |
| `401` | `API_KEY_MISSING` | Map or street view without an API key, or Lookup CNPJ without a key and with `expand` | Send the API key in `Authorization`, as explained in [First request](https://cnpja.com/en-us/docs/api/first-request) |
| `402` | `CACHED_QUERY_QUOTA_EXCEEDED`, `LIVE_QUERY_QUOTA_EXCEEDED` or `OVERAGE_BUDGET_EXCEEDED` | The organization's quota or overage budget is used up. What each code means is in [Error codes](https://cnpja.com/en-us/docs/api/limits-and-errors#error-codes) | Follow one of the ways out in [Quota used up](https://cnpja.com/en-us/docs/billing/quota-exhausted#ways-out) |
| `404` | `ESTABLISHMENT_NOT_FOUND` | The CNPJ is not in the Receita Federal registry | Check the CNPJ. Do not repeat the request right away, since each `404` is charged as a live query |

The other errors and the retry rules are in [Limits and errors](https://cnpja.com/en-us/docs/api/limits-and-errors).

## Use cases

- **Customer and supplier map:** place your portfolio on your own map with the coordinates of each CNPJ and identify concentrations by region.
- **Logistics and service areas:** calculate the distance between the establishment and a distribution center to set shipping, lead time or the responsible representative.
- **Sales visits:** find the establishments in a region with [CNPJ search](https://cnpja.com/en-us/docs/api/cnpj-search), look up each CNPJ with `expand=address.coordinates` and plan the visit routes from the coordinates.
- **Support for registration checks:** use the street view, or the map with `SATELLITE`, to check whether the stated address matches a property consistent with the activity, as part of onboarding a new customer.
- **Customer record:** show the map or the street view on the customer record in your system, within the [image usage rules](https://cnpja.com/en-us/docs/data/location#usage-rules).

## In the app

On a CNPJ's record, the **Geolocation** section shows the coordinates and their **Precision** and renders the aerial map and the street view only when you click **Load**, and only at **Address** precision, as explained in [Map, street view and coordinates](https://cnpja.com/en-us/docs/companies/location). To recompute the coordinates, use the section's refresh button; **Update all** and [automatic updates](https://cnpja.com/en-us/docs/companies/live-refresh#automatic-updates) only calculate them when the record has none yet. In exports, the coordinates are the **Address coordinates** resource, described in [Export to a spreadsheet](https://cnpja.com/en-us/docs/lists/export#resources).

## Notes

- The coordinates are only on Lookup CNPJ. [Lookup company](https://cnpja.com/en-us/docs/api/reference#tag/companies/GET/v2/companies/%7bid%7d) and [Search CNPJ](https://cnpja.com/en-us/docs/api/reference#tag/establishments/GET/v2/establishments) neither return nor filter by them, and the [CEP](https://cnpja.com/en-us/docs/api/postal-codes) operations do not return coordinates either.
- Map and street view do not accept public requests, and Lookup CNPJ without a key does not accept `expand`, so none of the three forms of location is available without a key. See [Public access](https://cnpja.com/en-us/docs/api/public-access).
- The address of [state registrations](https://cnpja.com/en-us/docs/api/state-registrations) and of [SUFRAMA](https://cnpja.com/en-us/docs/api/suframa) comes from other registries and is not used for the coordinates or the images.
- All parameters are in [Lookup CNPJ](https://cnpja.com/en-us/docs/api/reference#tag/establishments/GET/v2/establishments/%7btaxId%7d), [Render CNPJ map](https://cnpja.com/en-us/docs/api/reference#tag/establishments/GET/v2/establishments/%7btaxId%7d/map) and [Render CNPJ street view](https://cnpja.com/en-us/docs/api/reference#tag/establishments/GET/v2/establishments/%7btaxId%7d/street-view).
