[Developer tip] How to upload a file attachment to a task

I was recently asked how to use the API to add a file attachment to an Asana task. I know from the API forum section that folks have had some struggles with this, so I wanted to post some working code. Here you go!

[One thing to note that trips some people up: the name of the file as sent to Asana must be the literal string “file”. AFAIK any other string for the filename will fail.]


Python version:

import requests

def upload_asana_attachment(task_gid, asana_auth_token, file_path):
    try:
        url = f"https://app.asana.com/api/1.0/tasks/{task_gid}/attachments"
        headers = {
            "accept": "application/json",
            "Authorization": f"Bearer {asana_auth_token}"
        }
        files = {
            "file": open(file_path, "rb")
        }

        response = requests.post(url, headers=headers, files=files)
        print(response.content.decode('utf-8'))
    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    task_gid = "1234567890"
    asana_auth_token = "[your auth token goes here]"
    file_path = "C:\\Downloads\\image_test.png"  # complete path and filename goes here

    upload_asana_attachment(task_gid, asana_auth_token, file_path)
    

C# version:

using RestSharp;

namespace UploadAsanaAttachment;

internal class Program
{
    static async Task Main(string[] args)
    {
        try
        {
            var taskGid = "1234567890";
            var asanaAuthToken = "[your auth token goes here]";
            var url = "https://app.asana.com/api/1.0/tasks/" + taskGid + "/attachments";
            var filePath = "C:\\Downloads\\image_test.png";  // complete path and filename goes here

            var options = new RestClientOptions(url);
            var client = new RestClient(options);
            var request = new RestRequest("", Method.Post)
            {
                AlwaysMultipartFormData = true
            };

            request.AddHeader("accept", "application/json");
            request.AddHeader("Authorization", "Bearer " + asanaAuthToken);
            request.AddFile("file", filePath);

            var response = await client.ExecuteAsync(request);
            Console.WriteLine(response.Content);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

    }
}

Visual Basic version:

        Try
            Dim idTask = "1234567890"
            Dim asanaKey = "[your auth token goes here]"
            Dim url As String = "https://app.asana.com/api/1.0/tasks/" & idTask & "/attachments"
            Dim filePath = "C:\Downloads\image_test.png"  ' complete path and filename goes here

            Dim options As New RestClientOptions(url)
            Dim client As New RestClient(options)
            Dim request As New RestRequest("", Method.Post) With {
                    .AlwaysMultipartFormData = True
                    }

            request.AddHeader("accept", "application/json")
            request.AddHeader("Authorization", "Bearer " & asanaKey)
            request.AddFile("file", filePath)

            Dim response As RestResponse = Await client.ExecuteAsync(request)
            Console.WriteLine(response.Content)
        Catch e As Exception
            Console.WriteLine(e.ToString())
        End Try

6 Likes

Phil, saving the day, as always. Thanks man that’s awesome.