Here is a sample application that creates a new task named “Hello World!” and assigns it to a user.

First, we set up some constants to deal with values that will depend on who is using this script. These include the user-specific API key, the ID of the workspace in which the new task will be created, and the email address of the user to whom it will be assigned.

Next, we set up our HTTP connection. Note that the API only accepts secure connections via HTTPS.

Then we can start building the request. The most important part is setting the Authorization header to provide the API key (more details in the Authentication section below).

All API POST and PUT requests expect a top-level object with a single element in it named data. Successful responses will mirror this format. The data element should itself be an object, containing the parameters for the request. In the case of creating a new task, these are the fields we want to set on the task itself.

If the request was successful, we will get a response code of 201 indicating the object was created. That response will have a data field at its top level, which will contain complete information on the new task, including its ID.

If something went wrong during the request, we’ll get a different status code and the JSON returned will contain an errors field at the top level containing a list of problems. We look at the first one and print out its message.

#!/usr/bin/env ruby

require "rubygems"
require "JSON"
require "net/https"
personal_access_token = PERSONAL-ACCESS-TOKEN
workspace_id = WORKSPACE-ID
assignee = ASSIGNEE-EMAIL

# set up HTTPS connection
uri = URI.parse("https://app.asana.com/api/1.0/tasks")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER

# set up the request
header = {
  "Content-Type" => "application/json"
}

req = Net::HTTP::Post.new(uri.path, header)
req.basic_auth(personal_access_token, '')
req.body = {
  "data" => {
    "workspace" => workspace_id,
    "name" => "Hello World!",
    "assignee" => assignee
  }
}.to_json()

# issue the request
res = http.start { |http| http.request(req) }

# output
body = JSON.parse(res.body)
if body['errors'] then
  puts "Server returned an error: #{body['errors'][0]['message']}"
else
  puts "Created task with id: #{body['data']['id']}"
end