Javascript Examples

In most cases, we recommend using our Autocomplete Element when making requests from a browser.

Sometimes, though, it can be helpful to have very simple examples for how to get started, without bringing in any extra tooling. The examples below contain simple Javascript snippets that send an Autocomplete request to Geocode Earth.

Fetch #

var api_key = '<YOUR API KEY>'
var url = `https://api.geocode.earth/v1/autocomplete?api_key=${api_key}&text=london`

var options = {
  method: 'GET',
  headers: {
    'Accept': 'application/json'
  }
}

fetch(url, options)
  .then(res => console.log('success:', res))
  .catch(error => console.error('error:', error))

XMLHttpRequest #

var api_key = '<YOUR API KEY>'
var url = `https://api.geocode.earth/v1/autocomplete?api_key=${api_key}&text=london`

var xhr = new XMLHttpRequest()
xhr.open('GET', url, true)
xhr.onload = () => console.log("success", xhr.response)
xhr.onerror = () => console.error("error", xhr.response)

xhr.setRequestHeader('Accept', 'application/json')
xhr.send()