Getting started with JavaScript

Company Data API querying example with JavaScript

Request code

require('isomorphic-fetch');

API_KEY = '<REPLACE_WITH_YOUR_KEY>';
API_ENDPOINT = 'https://api.craft.co/v1/query';
GRAPHQL_QUERY = 'query getCompany($domain: String!) { company(domain: $domain) { locations { city, country } } }';

requestHeaders= { 'Content-Type': 'application/json', 'x-craft-api-key': API_KEY };
requestData = { 'query': GRAPHQL_QUERY, 'variables': { 'domain': 'meta.com' } };

fetch(API_ENDPOINT, {
    method: 'POST',
    headers: requestHeaders,
    body: JSON.stringify(requestData),
})
    .then(function(response)  { return response.json() })
    .then(console.log)
    .catch(console.error);

Make sure to replace <REPLACE_WITH_YOUR_KEY> with your unique API key.

Response

The above command returns JSON structured like this:

{
  "data": {
    "company": {
      "locations": [
        {
          "city": "Tokyo",
          "country": "JP"
        },
        {
          "city": "Ottawa",
          "country": "CA"
        },
        {
          "city": "Jakarta",
          "country": "ID"
        },
       ... 
      ]
    }
  }
}

Read more

Last updated