Costa Rica postal code API examples
Address forms, delivery pricing and place search. Everything runs in the browser with no API key: open CORS.
See the examplesView on GitHub
- 13
- endpoints
- 494
- districts
- 6,891
- towns and beaches
One postal code in one line
From the browser, Node or Deno: one fetch. With curl and jq, from the terminal.
// JavaScript
const d = await fetch('https://api.postalcr.dev/distritos/10305.json').then((r) => r.json());
d.etiqueta; // "San Antonio, Desamparados, San José"
d.codigo_postal; // "10305"
# curl
curl -s https://api.postalcr.dev/distritos/10305.json | jq -r .etiqueta
# San Antonio, Desamparados, San JoséCascading address selects
Province → canton → district, and the postal code fills itself. Three <select> and one field for the code.
HTML ids: provincia, canton, distrito and codigoPostal.
const API = 'https://api.postalcr.dev';
const get = (ruta) => fetch(API + ruta).then((r) => r.json());
const llenar = (sel, items, valor, texto) => sel.replaceChildren(
new Option('—', ''),
...items.map((i) => new Option(i[texto], i[valor])));
const provincias = await get('/provincias.json');
llenar(provincia, provincias, 'codigo', 'nombre');
provincia.onchange = async () => {
const p = await get(`/provincias/${provincia.value}.json`);
llenar(canton, p.cantones, 'codigo', 'nombre');
};
canton.onchange = async () => {
const c = await get(`/cantones/${canton.value}.json`);
llenar(distrito, c.distritos, 'codigo_postal', 'nombre');
};
distrito.onchange = () => (codigoPostal.value = distrito.value);Delivery pricing by GAM
Many shops charge differently inside and outside the Greater Metropolitan Area. /gam.json lists the codes on each side.
Each courier may use its own zone list: this is the official GAM Plan definition.
const { gam } = await fetch('https://api.postalcr.dev/gam.json').then((r) => r.json());
const dentro = new Set(gam);
const zona = (codigoPostal) => (dentro.has(codigoPostal) ? 'GAM' : 'Fuera de la GAM');
zona('10203'); // "GAM" San Rafael, Escazú
zona('60106'); // "Fuera de la GAM" Manzanillo, PuntarenasFind a town or a beach
Many people type the town or beach, not the district. /pueblos.json carries each place’s code; the search ignores accents.
const pueblos = await fetch('https://api.postalcr.dev/pueblos.json').then((r) => r.json());
const plano = (s) => s.normalize('NFD').replace(/\p{M}/gu, '').toLowerCase();
const buscar = (q) =>
pueblos.filter((p) => plano(p.nombre).includes(plano(q)))
.map((p) => `${p.nombre}: ${p.codigo_postal}`);
buscar('tamarindo');
// ["Finca Tamarindo: 61004", "Playa Tamarindo: 50309",
// "Playa Tamarindo: 60105", "Tamarindo: 50309",
// "Tamarindo: 50401", "Tamarindo: 61004"]Postal codes in Python
With requests (or any HTTP client). Field names are the same in every language.
import requests
API = "https://api.postalcr.dev"
d = requests.get(f"{API}/distritos/10305.json", timeout=10).json()
print(d["etiqueta"], d["codigo_postal"])
# San Antonio, Desamparados, San José 10305
gam = set(requests.get(f"{API}/gam.json", timeout=10).json()["gam"])
print("10203" in gam)
# TrueA district map
distritos.geojson loads straight into Leaflet, MapLibre or D3. Each shape carries the code, the label and its GAM flag.
// Leaflet
const geo = await fetch('https://api.postalcr.dev/distritos.geojson').then((r) => r.json());
L.geoJSON(geo, {
style: (f) => ({ weight: 1, fillOpacity: f.properties.gam ? 0.5 : 0.1 }),
onEachFeature: (f, capa) =>
capa.bindPopup(`${f.properties.etiqueta}: ${f.properties.codigo_postal}`),
}).addTo(mapa);