Getting clientId and authToken

  1. Login to your system as administrator via browser and go to: https://your_domain/apiClient/list or from menu: Settings->Synchronization->Manage API clients
  2. Click 'create new`
  3. Type API client name and check 'Active' checkbox
  4. Save
  5. Copy values: Name as clientId and Secret as authToken

Getting a token:

function getToken() {
  return new Promise(function (resolve, reject) {
    fetch("/security/login", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        // Client name
        clientId: "sample_client",
        // Client token
        authToken:
          "29c327e0ed2a5d22f8faa9edbab7ee400951502828172eaa5571d311da5911d6",
        // User login
        login: "test",
        // User password
        password: "test1",
      }),
    })
      .then(function (response) {
        // Login failed
        if (response.status !== 200) {
          reject(response);
        }

        // Examine the text in the response - return token
        response.json().then(function (data) {
          resolve(data.token);
        });
      })
      .catch(function (err) {
        reject("Fetch Error :-S", err);
      });
  });
}

Use a token to get products:

function getProducts(token) {
  return new Promise(function (resolve, reject) {
    fetch("/api/products", {
      method: "GET",
      headers: {
        "Content-Type": "application/json",
        "Authorization": "Bearer " + token,
      },
    })
      .then(function (response) {
        resolve(response);

        // ...
      })
      .catch(function (err) {
        reject("Fetch Error :-S", err);
      });
  });
}

getToken().then(function (token) {
  getProducts(token);
});