API Documentation - Endpoints & Examples

Select Countries

Get all countries optimized for select dropdowns and forms

The /v1/select/countries endpoint returns a lightweight list of all countries with only id and name fields, optimized for dropdown menus and form inputs.

Endpoint

GET https://api.countrydataapi.com/v1/select/countries

Authentication

Include your API key as a query parameter:

?apikey=your-api-key

Your API key is like a password, keep it secure. Get your key from the account dashboard.

Query Parameters

Parameter Type Required Description
apikey string Yes Your API authentication key
lang string No Language code for country names (default: en)

Supported Languages

  • en - English (default)
  • es - Spanish
  • pt - Portuguese
  • fr - French
  • de - German
  • it - Italian

Request Example

curl "https://api.countrydataapi.com/v1/select/countries?apikey=your-api-key&lang=en"

JavaScript (Fetch)

const API_KEY = 'your-api-key';

async function loadCountries() {
  const response = await fetch(
    `https://api.countrydataapi.com/v1/select/countries?apikey=${API_KEY}&lang=en`
  );
  const data = await response.json();
  return data;
}

JavaScript (Axios)

import axios from 'axios';

const response = await axios.get(
  'https://api.countrydataapi.com/v1/select/countries',
  {
    params: {
      apikey: 'your-api-key',
      lang: 'en',
    },
  }
);

Python

import requests

response = requests.get(
    'https://api.countrydataapi.com/v1/select/countries',
    params={
        'apikey': 'your-api-key',
        'lang': 'en'
    }
)
data = response.json()

PHP

<?php
$apiKey = 'your-api-key';
$url = "https://api.countrydataapi.com/v1/select/countries?apikey={$apiKey}&lang=en";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);
?>

Angular

// country.service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

interface Country {
  id: string;
  name: string;
}

interface ApiResponse {
  success: boolean;
  data: Country[];
}

@Injectable({
  providedIn: 'root'
})
export class CountryService {
  private readonly API_KEY = 'your-api-key';
  private readonly BASE_URL = 'https://api.countrydataapi.com/v1';

  constructor(private http: HttpClient) {}

  getCountries(lang: string = 'en'): Observable<Country[]> {
    const params = new HttpParams()
      .set('apikey', this.API_KEY)
      .set('lang', lang);

    return this.http.get<ApiResponse>(
      `${this.BASE_URL}/select/countries`,
      { params }
    ).pipe(
      map(response => response.data)
    );
  }
}

// country-select.component.ts
import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { CountryService } from './country.service';

@Component({
  selector: 'app-country-select',
  standalone: true,
  imports: [CommonModule, FormsModule],
  template: `
    <div *ngIf="loading">Loading countries...</div>
    <div *ngIf="error" class="error">{{ error }}</div>
    <select
      *ngIf="!loading && !error"
      [(ngModel)]="selectedCountry"
      (ngModelChange)="onCountryChange($event)"
    >
      <option value="">Select a country...</option>
      <option *ngFor="let country of countries" [value]="country.id">
        {{ country.name }}
      </option>
    </select>
  `
})
export class CountrySelectComponent implements OnInit {
  countries: any[] = [];
  selectedCountry = '';
  loading = true;
  error: string | null = null;

  constructor(private countryService: CountryService) {}

  ngOnInit() {
    this.countryService.getCountries('en').subscribe({
      next: (data) => {
        this.countries = data;
        this.loading = false;
      },
      error: (err) => {
        this.error = 'Failed to load countries';
        this.loading = false;
      }
    });
  }

  onCountryChange(countryId: string) {
    console.log('Selected country:', countryId);
    // Trigger state loading, emit event, etc.
  }
}

Response Format

Success Response

{
  "success": true,
  "data": [
    {
      "id": "66c7a6c9e4bda21f4ab10ef2",
      "name": "Afghanistan"
    },
    {
      "id": "66c7a6c9e4bda21f4ab10ef3",
      "name": "Albania"
    },
    {
      "id": "66c7a6c9e4bda21f4ab10ef4",
      "name": "Algeria"
    }
    // ... more countries (200+ total)
  ]
}

Response Fields

Field Type Description
success boolean Indicates if the request was successful
data array Array of country objects
data[].id string Unique country identifier (MongoDB ObjectId)
data[].name string Country name in the requested language

Error Response

{
  "success": false,
  "error": {
    "code": "INVALID_API_KEY",
    "message": "The provided API key is invalid"
  }
}

See the Error Codes documentation for all possible error codes.

Response Size

The select endpoint is optimized for minimal bandwidth:

  • Full endpoint (/v1/countries/all): ~500KB (includes all country data)
  • Select endpoint (/v1/select/countries): ~15KB (only id and name)

This makes the select endpoint 97% smaller and perfect for form dropdowns!

Token Usage

This endpoint consumes 1 token per request.

Tip: Cache the response in localStorage or your application state. Countries rarely change, so you can safely cache for 7+ days.

Use Cases

1. Dropdown Menu

<select id="country">
  <option value="">Select country...</option>
  <!-- Populated via JavaScript -->
</select>

<script>
async function populateCountries() {
  const response = await fetch(
    'https://api.countrydataapi.com/v1/select/countries?apikey=your-api-key&lang=en'
  );
  const { data } = await response.json();

  const select = document.getElementById('country');
  data.forEach(country => {
    const option = new Option(country.name, country.id);
    select.add(option);
  });
}

populateCountries();
</script>

2. React Component

import { useState, useEffect } from 'react';

function CountrySelect() {
  const [countries, setCountries] = useState([]);

  useEffect(() => {
    fetch('https://api.countrydataapi.com/v1/select/countries?apikey=your-api-key&lang=en')
      .then(res => res.json())
      .then(({ data }) => setCountries(data));
  }, []);

  return (
    <select>
      <option value="">Select country...</option>
      {countries.map(country => (
        <option key={country.id} value={country.id}>
          {country.name}
        </option>
      ))}
    </select>
  );
}

3. Vue Component

<script setup>
import { ref, onMounted } from 'vue';

const countries = ref([]);

onMounted(async () => {
  const response = await fetch(
    'https://api.countrydataapi.com/v1/select/countries?apikey=your-api-key&lang=en'
  );
  const { data } = await response.json();
  countries.value = data;
});
</script>

<template>
  <select>
    <option value="">Select country...</option>
    <option v-for="country in countries" :key="country.id" :value="country.id">
      {{ country.name }}
    </option>
  </select>
</template>

Caching Example

Cache countries in localStorage to minimize API calls:

const CACHE_KEY = 'countries_cache';
const CACHE_DURATION = 7 * 24 * 60 * 60 * 1000; // 7 days

async function getCountries() {
  // Check cache first
  const cached = localStorage.getItem(CACHE_KEY);
  if (cached) {
    const { data, timestamp } = JSON.parse(cached);
    if (Date.now() - timestamp < CACHE_DURATION) {
      return data;
    }
  }

  // Fetch from API
  const response = await fetch(
    'https://api.countrydataapi.com/v1/select/countries?apikey=your-api-key&lang=en'
  );
  const { data } = await response.json();

  // Save to cache
  localStorage.setItem(CACHE_KEY, JSON.stringify({
    data,
    timestamp: Date.now()
  }));

  return data;
}

Multilingual Example

Switch between languages dynamically:

function loadCountries(language = 'en') {
  return fetch(
    `https://api.countrydataapi.com/v1/select/countries?apikey=your-api-key&lang=${language}`
  ).then(res => res.json());
}

// Load in Spanish
const spanishCountries = await loadCountries('es');

// Load in French
const frenchCountries = await loadCountries('fr');

Performance Tips

  1. Cache aggressively: Countries don't change often
  2. Use lazy loading: Load countries when dropdown is focused
  3. Implement search: For better UX with 200+ countries
  4. Preload on page load: Fetch in background while user reads content

Rate Limits

  • Free tier: 100 requests/day
  • Basic tier: 1,000 requests/day
  • Pro tier: 10,000 requests/day
  • Enterprise tier: Unlimited

Check our pricing page for more details.

Related Endpoints

Complete Integration Guides

Need Help?


Note: The id field returned is a MongoDB ObjectId that uniquely identifies each country across all endpoints. Use this ID when querying states, cities, or other related data.