Getting started overview

Getting started with API Grátis involves a sequence of steps designed to provide access to its public Brazilian data endpoints. Users typically begin by creating an account on the official API Grátis website to establish their identity and access the developer dashboard. Following account creation, the next step is to generate or locate the necessary API credentials, which serve as authentication tokens for all subsequent API calls. With credentials in hand, developers can then proceed to make their initial API request to confirm connectivity and data retrieval capabilities. The platform supports various programming languages through SDKs and direct HTTP requests, allowing for flexible integration. This process is documented to guide users through initial setup and common integration patterns, as detailed in the API Grátis official documentation.

A high-level overview of the getting started process:

  1. Account Creation: Register on the API Grátis platform.
  2. Credential Retrieval: Obtain your unique API key from the dashboard.
  3. First API Call: Execute a basic request to a public endpoint.

This structured approach ensures that developers can quickly move from registration to making successful API calls, enabling the integration of Brazilian public data into their applications.

Getting Started Quick Reference
Step What to do Where
1. Sign Up Register for a new account or log in. API Grátis homepage
2. Get API Key Navigate to your dashboard to find or generate your API key. API Grátis dashboard (after login)
3. Make Request Use a tool like cURL or an SDK to send your first API call. API Grátis documentation for endpoints
4. Explore Endpoints Review available APIs for CPF, CNPJ, CEP, etc. API Grátis API reference

Create an account and get keys

To begin using API Grátis, the first step is to create an account. This process typically involves navigating to the API Grátis homepage and selecting a registration option. Account creation usually requires providing an email address, setting a password, and agreeing to the terms of service. Once registered, users gain access to a developer dashboard or portal.

Within this dashboard, API keys are managed. An API key is a unique identifier used to authenticate requests to the API. It functions as a token that the API Grátis system uses to verify the identity of the calling application and to track usage against the user's account plan, including the free tier with up to 300 requests per month. Users can typically find their API key in a dedicated section of the dashboard, often labeled "API Keys," "Credentials," or "Settings." If a key is not automatically generated upon registration, there is usually an option to generate a new one. It is recommended to keep API keys secure and not expose them in client-side code or public repositories.

The process for obtaining an API key generally follows these steps:

  1. Visit the API Grátis website.
  2. Click on "Cadastre-se" (Sign Up) or "Login" if you already have an account.
  3. Complete the registration form with your details.
  4. Log in to your newly created account.
  5. Navigate to the "Minha Conta" (My Account) or "API Keys" section in your dashboard.
  6. Locate and copy your API key. This key will be required for all subsequent API requests.

Securing your API keys is a critical practice for any API integration. Best practices for API key management include:

  • Environment Variables: Store API keys as environment variables rather than hardcoding them directly into your application code. This prevents accidental exposure in version control systems.
  • Server-Side Access: Make API calls from your server-side application where possible, rather than directly from client-side code (e.g., JavaScript in a browser), to minimize exposure.
  • Rotation: Periodically rotate your API keys, generating new ones and revoking old ones, especially if there's any suspicion of compromise.

For more detailed guidance on API key security, consult general API security principles such as those outlined by Google Cloud's API key best practices.

Your first request

Once you have your API key, you can make your first request to an API Grátis endpoint. The API Grátis platform offers various endpoints for Brazilian public data, such as CPF (Individual Taxpayer Registry), CNPJ (National Registry of Legal Entities), and CEP (Brazilian Postal Code). For this example, we will use a common endpoint like the CEP lookup, as it is simple and does not require sensitive input data beyond the CEP itself.

API Grátis uses a RESTful architecture, meaning you interact with it using standard HTTP methods like GET. Authentication is typically handled by including your API key in the request, often as a query parameter or a header, as described in the API Grátis API reference.

A typical API endpoint for a CEP lookup might look like this (replace YOUR_API_KEY and CEP_NUMBER with your actual key and a valid Brazilian CEP):

GET https://api.apigratis.com.br/v1/cep/01001000?api_key=YOUR_API_KEY

To execute this request, you can use a command-line tool like cURL, which is pre-installed on most Unix-like operating systems and available for Windows. Here's an example cURL command:

curl -X GET "https://api.apigratis.com.br/v1/cep/01001000?api_key=YOUR_API_KEY"

Remember to replace YOUR_API_KEY with the actual API key you obtained from your dashboard. A valid Brazilian CEP like 01001000 (referring to Praça da Sé in São Paulo) is used in this example. Upon a successful request, the API will return a JSON object containing the address details associated with that CEP.

Example successful JSON response (simplified):

{
  "cep": "01001-000",
  "logradouro": "Praça da Sé",
  "complemento": "lado ímpar",
  "bairro": "Sé",
  "localidade": "São Paulo",
  "uf": "SP",
  "ibge": "3550308",
  "gia": "1004"
}

If you prefer to use one of the supported SDKs, the process will involve initializing the SDK with your API key and then calling the relevant method for the desired endpoint. API Grátis provides SDKs for Node.js, PHP, Python, Ruby, Go, Java, and C#.

For example, in Node.js, a request might look like this (conceptual, based on common SDK patterns):

const API_KEY = 'YOUR_API_KEY';
const APIGratisClient = require('apigratis-sdk-nodejs'); // Assuming an SDK import
const client = new APIGratisClient(API_KEY);

client.cep.lookup('01001000')
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error('Error fetching CEP:', error);
  });

Always refer to the specific SDK documentation for exact method names and usage details.

Common next steps

After successfully making your first API Grátis request, several common next steps can enhance your integration and utilization of the platform:

  1. Explore More Endpoints: Review the comprehensive list of available APIs in the API Grátis documentation. This includes endpoints for CPF, CNPJ, FIPE table, NCM, and holiday lookups. Understanding the full range of data available will help you identify additional integration opportunities.
  2. Integrate with an SDK: While direct HTTP requests are effective, using one of the official API Grátis SDKs (Node.js, PHP, Python, Ruby, Go, Java, C#) can streamline development. SDKs often handle authentication, request formatting, and response parsing, reducing boilerplate code.
  3. Implement Error Handling: Develop robust error handling in your application. The API Grátis documentation will detail various error codes and their meanings. Proper error handling ensures your application can gracefully manage issues like invalid requests, rate limits, or server errors. For general principles on API error handling, refer to resources like MDN Web Docs on HTTP Status Codes.
  4. Monitor Usage: Keep track of your API usage through the API Grátis dashboard. This is crucial for managing costs and ensuring you stay within the limits of your chosen plan, especially if you are on the free tier (300 requests/month) or a paid subscription.
  5. Consider Rate Limits: Be aware of the rate limits imposed by API Grátis to prevent abuse and ensure fair usage. Implement retry mechanisms with exponential backoff if your application frequently encounters rate limit errors.
  6. Upgrade Your Plan (If Needed): If your application's usage exceeds the free tier or your current paid plan limits, consider upgrading to a higher tier. API Grátis offers various pricing plans, starting from R$ 49.90/month for 5,000 requests.
  7. Secure Your API Key: Reiterate best practices for API key security, such as storing keys in environment variables and avoiding client-side exposure.

Troubleshooting the first call

When making your first API Grátis call, you might encounter issues. Here are common problems and their potential solutions:

  1. Invalid API Key:
    • Symptom: An authentication error, often an HTTP 401 Unauthorized or 403 Forbidden status code.
    • Solution: Double-check that your API key is correctly copied from your API Grátis dashboard. Ensure there are no leading or trailing spaces. Verify that you are including the API key in the request as specified in the API Grátis documentation (e.g., as a query parameter api_key).
  2. Incorrect Endpoint URL:
    • Symptom: HTTP 404 Not Found status code.
    • Solution: Confirm that the base URL and the specific endpoint path are correct. Compare it precisely with the examples provided in the API Grátis API reference. Pay attention to case sensitivity and any versioning (e.g., /v1/).
  3. Missing or Malformed Parameters:
    • Symptom: HTTP 400 Bad Request status code. The API response often includes a detailed error message.
    • Solution: Ensure all required parameters are present and correctly formatted. For a CEP lookup, this means providing a valid 8-digit Brazilian CEP number. Check data types (e.g., numbers vs. strings) and any specific formatting requirements (e.g., no hyphens in CEP if the API expects raw digits).
  4. Rate Limit Exceeded:
    • Symptom: HTTP 429 Too Many Requests status code.
    • Solution: This indicates you've surpassed the number of requests allowed within a specific timeframe for your current plan. Wait before making further requests, or consider upgrading your plan if this is a recurring issue. Implement a delay or exponential backoff in your code.
  5. Network Connectivity Issues:
    • Symptom: Connection timeouts or inability to reach the host.
    • Solution: Verify your internet connection. If running locally, check firewall settings. Ensure the API Grátis domain (api.apigratis.com.br) is reachable.
  6. JSON Parsing Errors:
    • Symptom: Your application fails to parse the API response, even if the HTTP status code is 200 OK.
    • Solution: This usually means the API returned valid JSON, but your parsing logic is incorrect, or the response format was unexpected. Print the raw API response to inspect its structure and adjust your parsing code accordingly.

Always consult the API Grátis documentation's error handling section for specific error codes and messages provided by the API.