Skip to content

DataAPI SDK english MT.DataAPI Basic usage

Taku AMANO edited this page Jul 5, 2013 · 10 revisions

Basic usage

Fetch public data

You can fetch public data (e.g. published entry), without authorization.

var api = new DataAPI({
  baseUrl:  "https://your-host/your-mt-api.cgi",
  clientId: "your-client-id"
});

api.listEntries(siteId, function(response) {
  if (response.error) {
    // Handle error
    return;
  }

  for (var i = 0; i < response.items; i++) {
      var entry = response.items[i];
      // Render an entry
  }
});

Fetch private data

You can fetch private data, with authorization.

var api = new DataAPI({
  baseUrl:  "https://your-host/your-mt-api.cgi",
  clientId: "your-client-id"
});

api.on("authorizationRequired", function(response) {
  // You will return to current URL after authorization succeeded.
  location.href = api.getAuthorizationUrl(location.href);
});

// This request requires authorization.
// If not authorized yet, a DataAPI object triggers the "authorizationRequired" event.
api.getUser('me', function(response) {
    // Render user data
});

api.listEntries(siteId, {status: 'Draft'}, function(response) {
  if (response.error) {
    // Handle error
    return;
  }

  // Fetched a list of drafts if you have a permission reading these.
  for (var i = 0; i < response.items; i++) {
      var entry = response.items[i];
      // Render an entry
  }
});
Clone this wiki locally