First API Call with JavaScript

We'll be walking through making your first API call with JavaScript.

Introduction to APIs

If you haven't already, we'd encourage you to [check out our step-by-step guide(https://youtu.be/_Wv3XGICXmU) to making API calls.

Below is a step-by-step guide to making a project, a batch, submitting tasks, and finalizing that batch.

const request = require('request');


const key = "YOUR_API_KEY_NO_COLON";
const projName = "zoo_2d";
const bathName = "zoo_2d_batch_1";
const callbackURL = "http://example.com";

/*
=================================
====Step 1: Create a project.====
=================================
step 1 can be done through your dashboard in the project page
*/

// url is defining what endpoint is being used
let url = "https://api.scale.ai/v1/batches";
let instructionDoc = "<iframe> instruction Iframe Url WIll Go Here <iframe>"

// payload defines what data is sent throught the API call
let payload = {
     'project': projName,
     'type': 'annotation',
     'params': {
       'instruction': instructionDoc
     }
  };

//this is where the POST requset to the Scale API is made
request.post({
        url: url,
        json: payload,
        auth: {
          user: key,
          pass: ''
        },
    }, (err, response, body) => {
        //this line will return an error code if the API call fails
        console.log(err);
        //this line will return the response from Scale
        console.log(JSON.parse(body));
    });
});

/*
=====================================
===Step 2: Prepare a batch upload.===
=====================================
*/

let url = "https://api.scale.ai/v1/batches";
let payload = {
  'project': projName,
  'name': bathName,
  'callback': callbackURL
};

request.post({
  url: url,
  json: payload,
  auth: {
    user: key,
    pass: ''
  },
}, (err, response, body) => {
  console.log(err);
  console.log(JSON.parse(body));
});

/*
=====================================
===Step 3: Add tasks to the batch.===
=====================================
*/

let url = "https://api.scale.ai/v1/task/annotation";
const attachments = [
'https://upload.wikimedia.org/wikipedia/commons/e/e6/Giraffes_at_west_midlands_safari_park.jpg',
 'https://upload.wikimedia.org/wikipedia/commons/1/14/Seals%40melb_zoo.jpg',
'https://upload.wikimedia.org/wikipedia/commons/thumb/a/a8/NCZooelephants.jpg/1280px-NCZooelephants.jpg'
];
let labels = ['person', 'giraffe', 'elephant', 'seal'];
let payload = {}

for (i = 0; i < attachments.length; i++) {
   payload = {
    'batch': bathName,
    'callbackURL': callbackURL,
    'attachment_type': 'image',
    'attachment': attachments,
    'objects_to_annotate': labels,
    'with_labels': true
  };


  request.post({
    url: url,
    json: payload,
    auth: {
      user: key,
      pass: ''
    },
  }, (err, response, body) => {
    console.log(err);
    console.log(JSON.parse(body));
  });

/*
// STEP 4 IS REQUIRED BEFORE YOUR TASKS CAN BE WORKED ON!
// Further details can be found in our API docs: https://scale.com/docs#batches
=================================
===Step 4: Finalize the batch!===
=================================
*/

let url = "https://api.scale.ai/v1/batches/"${bathName}"/finalize"

request.post({
  url: url,
  auth: {
    user: key,
    pass: ''
  },
}, (err, response, body) => {
  console.log(err);
  console.log(JSON.parse(body));
});

From these examples we can see the key parts of the API call are:

  • Your API key: LiveXXX vs. TestXXX
  • The endpoint URL: Used to define what actions you are trying to perform through the API call - all of the different endpoints with their descriptions can be found in our API docs
  • The Payload: Defines what data you are sending through the API call under the supported parameters for that endpoint - The parameters that are supported by each endpoints can be found in our API docs
Updated a year ago