-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcnpj.go
More file actions
67 lines (55 loc) · 1.47 KB
/
cnpj.go
File metadata and controls
67 lines (55 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
// FonteData - Consultar CNPJ em Go
// Docs: https://fontedata.com/docs
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
)
const apiKey = "fd_live_SUA_CHAVE"
const baseURL = "https://app.fontedata.com/api/v1/consulta"
func consultarCNPJ(cnpj string) (map[string]interface{}, error) {
cnpj = strings.NewReplacer(".", "", "/", "", "-", "").Replace(cnpj)
url := fmt.Sprintf("%s/consulta-cnpj-receita/%s", baseURL, cnpj)
client := &http.Client{Timeout: 30 * time.Second}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("X-API-Key", apiKey)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
fmt.Printf("Custo: R$ %s | Saldo: R$ %s\n",
resp.Header.Get("X-Request-Cost"),
resp.Header.Get("X-Balance-Remaining"))
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != 200 {
return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, body)
}
var result map[string]interface{}
if err := json.Unmarshal(body, &result); err != nil {
return nil, err
}
return result, nil
}
func main() {
cnpj := "00000000000191"
if len(os.Args) > 1 {
cnpj = os.Args[1]
}
dados, err := consultarCNPJ(cnpj)
if err != nil {
fmt.Fprintln(os.Stderr, "Erro:", err)
os.Exit(1)
}
fmt.Printf("Razao social: %v\n", dados["razao_social"])
fmt.Printf("Situacao: %v\n", dados["situacao_cadastral"])
fmt.Printf("CNAE: %v\n", dados["cnae_fiscal_descricao"])
}