Authentication overview

The ViaCep API provides a public service for looking up Brazilian postal codes (CEPs) and associated address details without requiring any form of API key, token, or secret. This design choice significantly simplifies the integration process for developers, allowing direct access to address data for various applications such as e-commerce platforms, logistics systems, and data validation tools. The service operates on an entirely unauthenticated model, which means that any client can send a request to the API endpoint and receive a response, provided the request adheres to the API's format and rate limitations. This approach contrasts with many other API services that utilize authentication mechanisms like OAuth 2.0 or API keys to control access, track usage, or enforce quotas. For ViaCep, the emphasis is on accessibility and ease of use for its specific public data domain, as detailed in the official ViaCep API documentation.

While the absence of explicit authentication simplifies development, it places a greater emphasis on client-side security and responsible usage to prevent abuse and ensure service availability. Developers integrating ViaCep should implement robust error handling and consider rate limiting mechanisms within their own applications to manage request volume proactively. The unauthenticated nature of the API makes it particularly suitable for public-facing applications where user interaction directly triggers address lookups, without exposing any sensitive credentials. The API's simplicity is a core feature, making it a popular choice for quick and efficient Brazilian address data retrieval.

Supported authentication methods

ViaCep exclusively supports unauthenticated access, meaning there are no traditional authentication methods like API keys, OAuth 2.0, or token-based authentication required or supported. This design choice is fundamental to the API's operation and accessibility. Developers interact with the API by making direct HTTP GET requests to specific endpoints, passing the CEP as a path parameter.

The table below summarizes the ViaCep API's approach to authentication:

Method When to Use Security Level Description
None (Unauthenticated) All ViaCep API requests Public access No credentials required. Direct HTTP GET requests to retrieve address data.

This model simplifies client-side implementation as there's no need to manage or securely store credentials. However, it also means that all requests are treated equally by the API, and the burden of preventing excessive or malicious usage falls primarily on the API provider's infrastructure and any implicit rate limiting it employs. Developers should be aware that while their requests are unauthenticated, standard web security practices, such as using HTTPS, are still crucial for protecting the integrity and confidentiality of the data in transit. For instance, the Mozilla Developer Network's guide to HTTPS explains the importance of secure communication protocols even for public data.

Getting your credentials

Since the ViaCep API operates entirely without authentication, there are no credentials to obtain, manage, or store. Developers can begin integrating the API immediately by constructing HTTP GET requests to the appropriate endpoints. This contrasts with services like Stripe's API key management, where developers must generate and securely handle publishable and secret keys.

To use ViaCep, you simply need to formulate the correct URL with the desired Brazilian postal code (CEP). For example, to query a specific CEP, you would make a request similar to https://viacep.com.br/ws/01001000/json/. There is no signup process, no dashboard to visit for key generation, and no authentication header to include in your requests. This straightforward approach eliminates the complexity associated with credential lifecycle management, such as key rotation, secure storage, and revocation processes.

This design choice allows for rapid prototyping and deployment, particularly for applications where the primary goal is quick access to public address information. While the lack of credentials simplifies development, it also means that usage is not tied to a specific developer account, which can impact how service providers monitor and manage abuse. Developers are encouraged to review the ViaCep documentation for any usage guidelines or implicit rate limits that may apply.

Authenticated request example

As ViaCep does not require authentication, all requests are made without any API keys, tokens, or authentication headers. The following examples demonstrate how to make a basic GET request to the ViaCep API using common programming languages. These examples assume you want to look up the address for the CEP 01001000, which corresponds to the central area of São Paulo, Brazil.

JavaScript (Fetch API)


fetch('https://viacep.com.br/ws/01001000/json/')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));

Python (Requests library)


import requests

cep = '01001000'
url = f'https://viacep.com.br/ws/{cep}/json/'

response = requests.get(url)

if response.status_code == 200:
    data = response.json()
    print(data)
else:
    print(f"Error: {response.status_code}")

PHP (cURL)


<?php
$cep = '01001000';
$url = "https://viacep.com.br/ws/{$cep}/json/";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
} else {
    $data = json_decode($response, true);
    print_r($data);
}

curl_close($ch);
?>

These examples illustrate the simplicity of interacting with the ViaCep API. In each case, a standard HTTP GET request is made to the service endpoint, and the response is a JSON object containing the address details. There are no additional headers for authentication or any special parameters required to gain access, aligning with the API's unauthenticated design philosophy. Developers can adapt these snippets to their preferred language and environment, focusing solely on parsing the response and integrating the address data into their applications.

Security best practices

While ViaCep does not require authentication, adopting robust security best practices remains crucial for developers. These practices focus on securing your application that consumes the API, protecting data in transit, and ensuring responsible usage to maintain service availability.

  • Always use HTTPS: Ensure all requests to viacep.com.br are made over HTTPS. This encrypts the communication between your application and the ViaCep server, protecting the integrity and confidentiality of the data exchanged from potential eavesdropping or tampering. Even though the data (address information) is public, using HTTPS prevents third parties from altering the response or observing which CEPS your application is querying. The IETF RFC 2818 on HTTP Over TLS outlines the importance of this protocol.
  • Implement client-side rate limiting: Although ViaCep offers unlimited free requests, continuous high-volume requests from a single source can lead to temporary blocking or service degradation. Implement client-side rate limiting or caching mechanisms within your application to prevent excessive requests for the same or recently queried CEPS. This reduces unnecessary load on the ViaCep service and enhances your application's performance. Consider using a caching layer for frequently accessed CEPs.
  • Validate and sanitize inputs: Before sending a CEP to the ViaCep API, validate that the input is in the correct format (e.g., 8 digits, optionally with a hyphen). Sanitize any user-provided input to prevent injection vulnerabilities in your own application, even if the ViaCep API itself is not susceptible to them. This ensures robust data handling within your system.
  • Handle errors gracefully: Design your application to handle API errors, such as invalid CEPs or temporary unavailability, gracefully. Implement retries with exponential backoff for transient errors to avoid overwhelming the API. Display user-friendly messages rather than raw error codes.
  • Monitor usage patterns: Keep track of your application's usage of the ViaCep API. While there are no explicit usage dashboards provided by ViaCep due to the unauthenticated nature, monitoring your outgoing request volume can help identify unexpected spikes or potential abuse from your end, allowing you to react quickly.
  • Protect your application's secrets (for other services): While ViaCep itself doesn't require secrets, your application likely interacts with other services that do. Always follow best practices for storing and accessing API keys, database credentials, and other sensitive information. Use environment variables, secret management services, or secure configuration files rather than hardcoding secrets directly into your codebase.
  • Stay informed about ViaCep updates: Regularly check the official ViaCep API documentation for any updates, changes in usage policies, or new best practices recommended by the service provider.

By adhering to these security and usage best practices, developers can ensure a reliable, secure, and responsible integration with the ViaCep API, benefiting from its public address lookup service while maintaining the stability of their applications and the broader ecosystem.