Tutorial: Publishing HITs with MTurk and the C# programming language

Amazon Mechanical Turk
Happenings at MTurk
4 min readJun 3, 2018

--

In a previous blog post, we shared a tutorial on how to get started with Amazon Mechanical Turk (MTurk) and the C# programming language as part of .NET. This blog post builds on that tutorial to show how to create Human Intelligence Tasks (HITs) in MTurk using the C# programming language and MTurk’s Application Programming Interface (API).

If you didn’t already step through the previous tutorial, we suggest you start there. You can find it here: https://blog.mturk.com/tutorial-using-the-mturk-api-with-the-aws-sdk-for-c-with-net-207fdec3bf75

Quick recap of where we left off

In the previous tutorial, we installed the AWS SDK for .NET using NuGet. When we left off in the previous tutorial, our code called MTurk’s API to invoke the GetAccountBalance method. By the end of the previous tutorial, our code, when run, would output this:

Your account balance is $406.75
Press any key to exit.

Changing out settings to use Sandbox

In the last tutorial, our code called GetAccountBalance and accessed the MTurk Production environment. For today’s tutorial, we’ll make a change to our code to access the MTurk Developer Sandbox environment. The MTurk Developer Sandbox environment is used for testing. HITs you create in this environment are unlikely to be completed by real Workers (since all reward amounts and payments are simulated). To make this change, edit the code from the previous tutorial as shown in bold below:

AmazonMTurkConfig config = new AmazonMTurkConfig();
config.ServiceURL = SANDBOX_URL;

you can confirm the change has been applied by running your code. You should now see an output that looks exactly like this (with a $10,000 simulated balance):

Your available balance is: $10000.00
Press any key to exit.

Creating our Question file

Let’s create a file called question.xml. We’ll create that file and place it inany directory we like, so long as we correctly refer to it’s location in the code below. In our case, we will place this in the c:\Users\johndoe directory. In the question.xml file, place the following code:

<HTMLQuestion xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2011-11-11/HTMLQuestion.xsd">
<HTMLContent><![CDATA[
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
<script type='text/javascript' src='https://s3.amazonaws.com/mturk-public/externalHIT_v1.js'> .
</script>
</head>
<body>
<form name='mturk_form' method='post' id='mturk_form' action='https://www.mturk.com/mturk/externalSubmit'>
<input type='hidden' value='' name='assignmentId' id='assignmentId'/>
<h1>This is a test question</h1>
<p><textarea name='comment' cols='80' rows='3'></textarea></p>
<p><input type='submit' id='submitButton' value='Submit' /></p></form>
<script language='Javascript'>turkSetAssignmentID();
</script>
</body>
</html>
]]>
</HTMLContent>
<FrameHeight>450</FrameHeight>
</HTMLQuestion>

Once you save this file, next we’ll edit our code to reference this file. To do this, take your code from the previous tutorial and add the line shown in bold below:

using System;
using Amazon.MTurk;
using Amazon.MTurk.Model;
namespace MTurkApp
{
class MTurkHelloWorld
{
static void Main(string[] args)
{
string awsAccessKeyId = "[YOUR_AWS_ACCESS_KEY_ID]";
string awsSecretAccessKey = "[YOUR_AWS_SECRET_KEY_ID]";
string SANDBOX_URL = "https://mturk-requester-sandbox.us-east-1.amazonaws.com";
string PROD_URL = "https://mturk-requester.us-east-1.amazonaws.com";
AmazonMTurkConfig config = new AmazonMTurkConfig();
config.ServiceURL = PROD_URL;
AmazonMTurkClient mturkClient = new AmazonMTurkClient(
awsAccessKeyId,
awsSecretAccessKey,
config);
GetAccountBalanceRequest request = new GetAccountBalanceRequest();
GetAccountBalanceResponse balance = mturkClient.GetAccountBalance(request);
Console.WriteLine("Your account balance is $" + balance.AvailableBalance); // Read the XML question file into a string
string questionXML = System.IO.File.ReadAllText(@"C:\Users\johndoe\question.xml");
Console.WriteLine("------");
Console.WriteLine(questionXML);
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}

Next run your code. You should now see exactly the following output in your console:

Your account balance is $10000.00
------
<HTMLQuestion xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2011-11-11/HTMLQuestion.xsd">
<HTMLContent>
<![CDATA[
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
<script type='text/javascript' src='https://s3.amazonaws.com/mturk-public/externalHIT_v1.js'> .
</script>
</head>
<body>
<form name='mturk_form' method='post' id='mturk_form' action='https://www.mturk.com/mturk/externalSubmit'>
<input type='hidden' value='' name='assignmentId' id='assignmentId'/>
<h1>This is a test question</h1>
<p><textarea name='comment' cols='80' rows='3'></textarea></p>
<p><input type='submit' id='submitButton' value='Submit' /></p></form>
<script language='Javascript'>turkSetAssignmentID();
</script>
</body>
</html>
]]>
</HTMLContent>
<FrameHeight>450</FrameHeight>
</HTMLQuestion>
Press any key to exit.

Excellent. You’ve successfully loaded the question.xml file into your C# program. Next, we’ll go ahead and use that file to create our HITs.

Now, let’s create some HITs

Now that we have our code calling the MTurk Developer Sandbox and we have loaded our question, we’ll be calling the CreateHIT API operation. To do this, simply add the lines of code shown in bold below to your program:

...
GetAccountBalanceRequest request = new GetAccountBalanceRequest();
GetAccountBalanceResponse balance = mturkClient.GetAccountBalance(request);
Console.WriteLine("Your account balance is $" + balance.AvailableBalance); // Read the XML question file into a string
string questionXML = System.IO.File.ReadAllText(@"C:\Users\johndoe\question.xml");
Console.WriteLine("------");
Console.WriteLine(questionXML);
// Create the HIT
CreateHITRequest hitRequest = new CreateHITRequest();
hitRequest.Title = "Test Title";
hitRequest.Description = "Test Description";
hitRequest.Reward = "0.50";
hitRequest.AssignmentDurationInSeconds = 60*60; //1 hr
hitRequest.LifetimeInSeconds = 60*60*24; // 1 day
hitRequest.Question = questionXML;
CreateHITResponse hit = mturkClient.CreateHIT(hitRequest);
// Show a link to the HIT
Console.WriteLine( "https://workersandbox.mturk.com/projects/" + hit.HIT.HITTypeId + "/tasks");
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
...

To keep your output clean, you can also comment out the lines that were displaying the question. This is optional but recommended. Simply update the two lines shown in bold below:

...
// Read the XML question file into a string
string questionXML = System.IO.File.ReadAllText(@"C:\Users\johndoe\question.xml");
//Console.WriteLine("------");
//Console.WriteLine(questionXML);
// Create the HIT
...

Now run your code. You should see the following output:

Your account balance is $10000.00
https://workersandbox.mturk.com/projects/3P8QI1T9B1R7DE2OP6ICO47FVRSOWF/tasks
Press any key to exit.

and if you open the link that’s displayed in your code’s output, you should see a screen that look similar to this:

Congratulations! You’ve successfully created a HIT using the MTurk API and the AWS SDK for C# in .NET.

Wrapping up

Today, we’ve just scratched the surface of what’s possible with the AWS SDK for C# in .NET and the MTurk Requester API. In future tutorials, we’ll continue to build on this to show readers how to do even more.

We hope you enjoyed today’s tutorial. If you have any questions, please post a question to our MTurk forums. You may also want to check out the AWS SDK for .NET documentation for MTurk here. To become a Requester, sign up here. Want to contribute as a Worker customer? Get started here.

--

--