Tutorial: Getting started with MTurk and Node.js

To help developers build and scale their use of Amazon Mechanical Turk (MTurk) we offer an Application Programming Interface (API) that can be accessed directly from the Amazon Web Services (AWS) Software Development Kits (SDKs). This means that developers can create and manage HITs, create and associate Worker Qualifications, and more in nine programming languages. These include Java, PHP, Ruby, Python, Go, and Node.js. You can learn more about these SDKs here.

Today, we’re going to share a brief tutorial on using MTurk with Node.js, a popular Javascript-based programming language.

Signing up and setting up your MTurk and AWS Accounts
In order to connect to MTurk with Node.js, you will need an MTurk Requester Account and an AWS account (these are two separate accounts).

If you don’t already have these accounts, begin by registering an MTurk Requester account at http://requester.mturk.com. Once this is complete, follow the Developer Getting Started Guide to sign up for an AWS account, an MTurk Developer Sandbox account, and link your MTurk and AWS accounts.

Best Practice: Use Identity and Access Management to access MTurk
You will use credentials from your AWS account when making API calls to securely authenticate yourself. The recommended way to do this is to use an AWS service called Identity and Access Management (IAM). You can create an IAM account and associate it with MTurk by following these steps. After you create the IAM user, keep its associated access key and secret key handy for the next step.

Setting up Node.js
If you don’t already have Node.js on your computer, your next step should be to download it. Here are some helpful links to help based on your operating system:

Alternatively, if you use Homebrew on your Mac, you can simply install Node by issuing the following command:

brew install node

Installing the AWS SDK
Once you have Node.js and the Node Package Manager (NPM) installed, you can install the AWS SDK, that includes support for MTurk, by issuing the following command:

npm install aws-sdk

Configuring the AWS SDK
Now, remember the credentials you saved when you completed the IAM step earlier? You should have an access key and a secret key handy. We’re going to use those to configure our AWS SDK. We do that by creating a credentials file at ~/.aws/credentials on Mac/Linux or C:\Users\USERNAME\.aws\credentials on Windows. Once created, you’ll put this in the file (replacing ‘your_access_key’ and ‘your_secret_key’ with the values from the IAM step).

[default]
aws_access_key_id = your_access_key
aws_secret_access_key = your_secret_key

Make your first call to MTurk using Node.js
You’ve now setup everything needed to make your first call to MTurk. In an earlier step, you setup your MTurk Requester Developer Sandbox account (where you’ll test things in a simulated environment), and another MTurk Requester Account to use in Production (with real Worker customers and real money).

Let’s make our first call in the MTurk Developer Sandbox. Place the following code in a file called hello_world.js.

This code is going to create an MTurk client, that you can use to call any MTurk API method. It’s then going to call the getAccountBalance method to retrieve your balance. In the MTurk Requester Developer Sandbox environment, the balance is always $10,000 (since, remember, the sandbox is a simulated environment where no real money actually moves and real Worker customers generally don’t complete HITs).

Save this file, and run the following command at the Command Prompt or Terminal:

node hello_world.js

You should see the following output:

{ AvailableBalance: '10000.00' }

Congrats! You’ve made your first MTurk API call using Node.js. You’ll notice that in the code above, there’s a line commented out that looks like this:

//var endpoint = 'https://mturk-requester.us-east-1.amazonaws.com';

To execute the same call in the MTurk Requester Production environment (the environment with real money and real Worker customers) simply remove the leading // characters and you’ll change environments. This is useful as it makes it easy to switch between the sandbox (where you’ll typically test) and production (where you’ll create and manage real tasks).

You can discover more API methods and the appropriate syntax in the AWS SDK documentation for JavaScript here: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/MTurk.html

If you have any questions, please post a question to our MTurk forums. To become a Requester, sign up here. Want to contribute as a Worker customer? Get started here.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.

Responses
The author has chosen not to show responses on this story. You can still respond by clicking the response bubble.