Welcome to country-state-city microservice

JavaScript Usage Examples

Here are some JavaScript examples using the Fetch API to interact with the endpoints.

1. Get Coords

Endpoint

GET https://api.geocodxyz.rebuscando.info/public/getCoords

Example

 

async function getCoordinates(postalAddress, apikey, zipcode = '', city = '', state = '', country = '') {
    const url = new URL('https://api.geocodxyz.rebuscando.info/public/getCoords'); // Replace with your actual API domain const params = { postaladdress: postalAddress, zipcode: zipcode, city: city, state: state, country: country, apikey: apikey }; Object.keys(params).forEach(key => url.searchParams.append(key, params[key])); try { const response = await fetch(url, { method: 'GET', headers: { 'Content-Type': 'application/json' } }); if (response.status === 200) { const data = await response.json(); console.log('Coordinates:', data); return data; } else if (response.status === 401) { console.error('Invalid API key.'); } else if (response.status === 500) { console.error('Internal server error.'); } else { console.error('Unexpected response:', response); } } catch (error) { console.error('Error fetching coordinates:', error); } } // Example usage const postalAddress = '123 Main St'; const apikey = 'your-api-key'; // Replace with your actual API key getCoordinates(postalAddress, apikey, '90210', 'Los Angeles', 'CA', 'USA'); 

2. Get Address

Endpoint

GET https://api.geocodxyz.rebuscando.info/api/public/getAddress

Example



async function getAddress(lat, lon, apikey) {
    const url = new URL('https://api.geocodxyz.rebuscando.info/public/getAddress'); // Replace with your actual API domain const params = { lat: lat, lon: lon, apikey: apikey }; Object.keys(params).forEach(key => url.searchParams.append(key, params[key])); try { const response = await fetch(url, { method: 'GET', headers: { 'Content-Type': 'application/json' } }); if (response.status === 200) { const data = await response.json(); console.log('Address:', data); return data; } else if (response.status === 401) { console.error('Invalid API key.'); } else if (response.status === 500) { console.error('Internal server error.'); } else { console.error('Unexpected response:', response); } } catch (error) { console.error('Error fetching address:', error); } } // Example usage const lat = -33.7774886; const lon = 151.0786394; const apikey = 'your-api-key'; // Replace with your actual API key getAddress(lat, lon, apikey); 

3. Upload file

Endpoint

POST https://geocod.xyz/api/public/uploadFIle

Example

 

async function uploadFile(file, apikey, reversed = false) {
    const url = 'http://api.geocodxyz.rebuscando.info/public/uploadFile'; // Replace with your actual API domain const formData = new FormData(); formData.append('file', file); formData.append('reversed', reversed); formData.append('apikey', apikey); try { const response = await fetch(url, { method: 'POST', body: formData }); if (response.status === 200) { const data = await response.json(); console.log('Response:', data); return data; } else if (response.status === 401) { console.error('Invalid API key.'); } else if (response.status === 406) { console.error('You must wait to upload another file.'); } else if (response.status === 500) { console.error('Internal server error.'); } else { console.error('Unexpected response:', response); } } catch (error) { console.error('Error uploading file:', error); } } // Example usage const fileInput = document.querySelector('input[type="file"]'); const apikey = 'your-api-key'; // Replace with your actual API key fileInput.addEventListener('change', (event) => { const file = event.target.files[0]; if (file) { uploadFile(file, apikey, true); // Set `reversed` to true or false as needed } }); 

 

These examples demonstrate how to make GET requests to each of the endpoints, handle the response, and manage errors. You can test these examples in a browser’s console or integrate them into your JavaScript application.