# Company Search

In the Commercial API, in the `Premium Plan`, you can perform searches using filters to find companies that meet your requirements.

{% hint style="warning" %}
**Attention:** Access to this endpoint is exclusive to Premium Plan subscribers.
{% endhint %}

**Method:** `GET`

**Endpoint:** `https://comercial.cnpj.ws/pesquisa?ADD_FILTERS`

### Filters

You can use the filters below in your queries:

| Field                          | Description                                                     |
| ------------------------------ | --------------------------------------------------------------- |
| atividade\_principal\_id       | CNAE Code                                                       |
| atividade\_secundaria\_id      | CNAE Code                                                       |
| atividade\_id                  | CNAE Code (search in the primary and secondary activity)        |
| natureza\_juridica\_id         | Legal Nature Code                                               |
| razao\_social                  | Company Name                                                    |
| nome\_fantasia                 | Trade Name                                                      |
| pais\_id                       | BACEN Country Code                                              |
| estado\_id                     | IBGE State Code                                                 |
| cidade\_id                     | IBGE City Code                                                  |
| cep                            | ZIP Code                                                        |
| situacao\_cadastral            | Registration status with the Federal Revenue                    |
| data\_situacao\_cadastral\_de  | Registration Status Date (From this date) in YYYY-MM-DD format  |
| data\_situacao\_cadastral\_ate | Registration Status Date (Until this date) in YYYY-MM-DD format |
| porte\_id                      | Company size ID                                                 |
| socio\_nome                    | Partner's Name                                                  |
| data\_inicio\_atividade\_de    | Start Date of Activity (From this date) in YYYY-MM-DD format    |
| data\_inicio\_atividade\_ate   | Start Date of Activity (Until this date) in YYYY-MM-DD format   |

It is crucial to highlight that the queries made in this endpoint are based on our database and not on the Federal Revenue database. Therefore, discrepancies may occur between the information, as our database may have a lag of up to 45 days compared to the updated data from the Federal Revenue. It is worth noting that, although we perform daily registrations due to CNPJ queries made in the API, the lag can still occur.

**List of Registration Statuses:**

* Active
* Closed
* Inactive
* Null
* Suspended

**List of Registered Sizes:**

| ID | Description      |
| -- | ---------------- |
| 01 | Not informed     |
| 02 | Micro Enterprise |
| 03 | Small Business   |
| 05 | Others           |

### Request Examples

{% openapi src="/files/pX97Qdj2qn3xNQOZEdoV" path="/pesquisa" method="get" %}
[openapi-comercial.yaml](https://content.gitbook.com/content/543mjjjP3f2OCfQR723f/blobs/Fw4vf2BKurgpfEERuww1/openapi-comercial.yaml)
{% endopenapi %}

We have a package that can help you integrate with JavaScript:

```shell
yarn add consultar-cnpj
```

```js
const consultarCNPJ = require("consultar-cnpj");

async function getCNPJ() {
  const token = "INFORME O SEU TOKEN DE ACESSO";
  const page = 2;

  const data = await consultarCNPJ.pesquisa(
    { atividade_principal_id: "6203100", estado_id: 28 },
    token,
    page
  );
  console.log(data);
}
```

[More information about the package](https://www.npmjs.com/package/consultar-cnpj)

#### Pagination

If the search returns more than 20 CNPJs, the API will divide the response into pages. You can check this in the API's JSON response, in the "pagination" property, which shows the current page, the total number of pages, and the total number of CNPJs:

```json
{
  "paginacao": {
    "limite": 20,
    "pagina": 1,
    "paginas": 5,
    "total": 87
  }
}
```

To search for a specific page, simply provide the page number in the request:

```shell
curl -X GET https://comercial.cnpj.ws/pesquisa?atividade_principal_id=6203100&pagina=2 -H "x_api_token: YOUR_TOKEN"
```

It is also possible to change the number of CNPJs returned, the default is 20 but it can be up to 100:

```shell
curl -X GET https://comercial.cnpj.ws/pesquisa?atividade_principal_id=6203100&pagina=2&limite=100 -H "x_api_token: YOUR_TOKEN"
```

#### Return Example

Below is an example of the JSON returned when searching by CNAE **6203100**:

```json
{
  "paginacao": {
    "limite": 20,
    "pagina": 1,
    "paginas": 1494,
    "total": 29865
  },
  "ordenacao": [],
  "filtros_disponiveis": [
    "cnpj",
    "cnpj_raiz",
    "natureza_juridica_id",
    "porte_id",
    "razao_social",
    "nome_fantasia",
    "pais_id",
    "estado_id",
    "cidade_id",
    "cep",
    "atividade_principal_id"
  ],
  "filtros_aplicados": {
    "atividade_principal_id": "6203100"
  },
  "data": [
    "19323263000160",
    "03124023000104",
    "64919913000199",
    "36427967000100",
    "06370042000109",
    "01056469000105",
    "03292219000108",
    "94338217000664",
    "01645901000101",
    "02868026000181",
    "04957691000177",
    "01755516000109",
    "04970015000133",
    "65144347000153",
    "04810384000169",
    "74641804000106",
    "82296112000104",
    "03035876000161",
    "33643305009801",
    "05121798000143"
  ]
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cnpj.ws/en/api-reference/api-comercial/pesquisa-de-empresas.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
