Searchmetrics Developers API

Examples

JavaScript example using apollo-fetch

For a node environment without import support simply replace the following import with require


import { createApolloFetch } from "apollo-fetch";

const SM_GRAPHQL_API = "https://graphql.searchmetrics.com/";

const apolloFetch = createApolloFetch({ uri: SM_GRAPHQL_API });

apolloFetch.use(({ request, options }, next) => {
  options.headers = {
    "sm-api-key": "$yourShineyKey",
    "sm-api-secret": "$yourDarkestSecret"
  };
  next();
});


const exampleQuery = `
    query {
        greeting {
            randomId
            who
        }
    }
`;


const exampleMutation = `
    mutation CreateGreetingMutation($template: String!) {
        create_greeting(template: $template) {
            randomId
            who
        }
    }
`;


const fetchData = () =>
  apolloFetch({
    query: exampleQuery
  }).catch(error => {
    console.log(error);
  });

fetchData().then(response => console.log(response));


const mutateData = () =>
  apolloFetch({
    query: exampleMutation, // yes a mutation is just a query
    variables: { template: "Have you met" }
  }).catch(error => {
    console.log(error);
  });

mutateData().then(response => console.log(response));