# Upload attachment using Python SDK v5

**URL:** <https://forum.asana.com/t/upload-attachment-using-python-sdk-v5/846077>\
**Category:** API & Developers\
**Tags:** attachments\
**Created:** [27 June 2024 05:18 UTC](https://forum.asana.com/t/upload-attachment-using-python-sdk-v5/846077 "2024-06-27T05:18:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Caleb\_Pearce](https://luna1.co/8d76cd.png) [@Caleb\_Pearce](https://forum.asana.com/u/Caleb_Pearce)\
**Post date:** [27 June 2024 05:18 UTC](https://forum.asana.com/t/upload-attachment-using-python-sdk-v5/846077/1 "2024-06-27T05:18:26Z")

</div>

Hi,

I’m having a couple issues attaching a pdf to an Asana task, using Python.

Starting with the code from here [Upload an attachment](https://developers.asana.com/reference/createattachmentforobject?_gl=1*pddurl*_gcl_au*MjU0ODU5Mzg2LjE3MTc3MTQwMzE.*FPAU*MTQ4MzE5Mzc2NS4xNzE3NzEzOTE4*_ga*MTUxNTE3MTkwOS4xNzE3NzEzOTE2*_ga_J1KDXMCQTH*MTcxOTQ2MzUyMy4xNS4xLjE3MTk0NjM1OTguMC4wLjMzMDQxMzQzNA..*_fplc*enF0Rk9sN0ZLU3YzTUFsRFdtTCUyRm1PVk42QVBIRDY5TWRZcWFSanBoUSUyRndKeEl1bXN6WGJlOTlLJTJCbGlYYnptMlBuQm1WSWZvc2tQTGwlMkZzSjVNZUtxR3Q0T294Z0xjeXQwRktKblV5U0ZUWlpSQnRFeFRGVDV5VEo3eG9EUGclM0QlM0Q.)

I ended up with the script below. It works if the file link is a web URL e.g. OneDrive (yay!).

First issue, I can’t work out what I’m doing wrong when I try to attach a file from my local hard drive. I’ve tried a few variations of formatting the "file:\" in front of the path but nothing worked.

Second, much smaller issue is Asana doesn’t seem to recognise the attachment as a PDF . It has a generic icon.

I’m new to this. Should I ditch sdk v5 and go to v3 which has more examples?

My script:

> import asana  
> from asana.rest import ApiException  
> from pprint import pprint
> 
> configuration = asana.Configuration()  
> configuration.access\_token = ‘token’  
> api\_client = asana.ApiClient(configuration)
> 
> # create an instance of the API class
> 
> attachments\_api\_instance = asana.AttachmentsApi(api\_client)  
> opts = {  
> ‘resource\_subtype’: “external”, # str |  
> #‘file’: “Z:\PU06-002 (8.3m x 2m - Triple Roller Drawings).pdf”, # str |  
> ‘parent’: “task gid”, # str |  
> ‘url’: “file:///Z:/CAD%20Database/PDF/100005A.RevB.RW%20Pivot%20Block%20Top\_Top.pdf”, # str |  
> ‘name’: “PU06-002.pdf”, # str |  
> #‘connect\_to\_app’: True, # bool |  
> #‘opt\_fields’: “connected\_to\_app,created\_at,download\_url,host,name,parent,parent.created\_by,parent.name,parent.resource\_subtype,permanent\_url,resource\_subtype,size,view\_url”, # list[str] | This endpoint returns a compact resource, which excludes some properties by default. To include those optional properties, set this query parameter to a comma-separated list of the properties you wish to include.  
> }
> 
> try:  
> # Upload an attachment  
> api\_response = attachments\_api\_instance.create\_attachment\_for\_object(opts)  
> pprint(api\_response)  
> except ApiException as e:  
> print(“Exception when calling AttachmentsApi-\>create\_attachment\_for\_object: %s\n” % e)

This is the response"

> Exception when calling AttachmentsApi-\>create\_attachment\_for\_object: (400)  
> Reason: Bad Request  
> HTTP response headers: HTTPHeaderDict({‘Content-Type’: ‘application/json; charset=UTF-8’, ‘Content-Length’: ‘269’, ‘Connection’: ‘keep-alive’, ‘Date’: ‘Thu, 27 Jun 2024 04:49:20 GMT’, ‘Server’: ‘nginx’, ‘cache-control’: ‘no-store’, ‘pragma’: ‘no-cache’, ‘x-frame-options’: ‘DENY’, ‘x-xss-protection’: ‘1; mode=block’, ‘x-content-type-options’: ‘nosniff’, ‘content-security-policy’: “report-uri [https://app.asana.com/-/csp\_report?report\_only=false;default-src](https://app.asana.com/-/csp_report?report_only=false;default-src) ‘none’;frame-src ‘none’;frame-ancestors ‘none’”, ‘x-asana-api-version’: ‘1.0’, ‘asana-change’: ‘name=new\_user\_task\_lists;info=[Update on our planned API changes to user task lists (a.k.a. My Tasks)](https://forum.asana.com/t/update-on-our-planned-api-changes-to-user-task-lists-a-k-a-my-tasks/103828), name=new\_goal\_memberships;info=[https://forum.asana.com/t/launched-team-sharing-for-goals/378601;affected=true](https://forum.asana.com/t/launched-team-sharing-for-goals/378601;affected=true)’, ‘x-robots-tag’: ‘none’, ‘X-Cache’: ‘Error from cloudfront’, ‘Via’: ‘1.1 [15f8211e7c4eeeaf8ff1b28a64d08eae.cloudfront.net](http://15f8211e7c4eeeaf8ff1b28a64d08eae.cloudfront.net) (CloudFront)’, ‘X-Amz-Cf-Pop’: ‘MEL50-C1’, ‘X-Amz-Cf-Id’: ‘Q\_2R8FpAZ6-mkL8qs1xXkPlnJ\_UQ74flahcU2N1DrCGNApMEeF6XCw==’, ‘Referrer-Policy’: ‘strict-origin-when-cross-origin’, ‘Strict-Transport-Security’: ‘max-age=31536000; includeSubDomains; preload’, ‘X-UA-Compatible’: ‘IE=edge,chrome=1’})  
> HTTP response body: b’{“errors”:[{“message”:“url: Invalid URL for link: file:///Z:/CAD%20Database/PDF/100005A.RevB.RW%20Pivot%20Block%20Top\_Top.pdf”,“help”:“For more information on API status codes and how to handle them, read the docs on errors: [Errors](https://developers.asana.com/docs/errors)”}]}’

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![David\_Neal](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/david_neal/32/242025_2.png) [@David\_Neal](https://forum.asana.com/u/David_Neal)\
**Post date:** [27 June 2024 20:31 UTC](https://forum.asana.com/t/upload-attachment-using-python-sdk-v5/846077/2 "2024-06-27T20:31:18Z")

</div>

Hi @Caleb_Pearce!

I don’t know Python very well, so I’m going to have to get back to you on that 🙂 However, I do know to get correct PDF icon to show up you’ll need to add a specific content-type to the request. In the [upload an attachment](https://developers.asana.com/reference/createattachmentforobject) docs, there’s a note on using curl.

> When uploading PDFs with curl, force the content-type to be pdf by appending the content type to the file path: `--form "file=@file.pdf;type=application/pdf"`.

So, I imagine you’ll need to do something to add the `type=application/pdf` to the upload. When using the JavaScript/Node.js `FormData` to upload a multi-part form, you would need to do something like:

```js
// fileContent is a readable file stream
const form = new FormData();
form.append( "parent", parentId );
form.append( "file", fileContent, {
  filename: fileName,
  type: "application/pdf"
} );

```

Hope this helps!

---

<div class="post-metadata">

**Author:** ![Caleb\_Pearce](https://luna1.co/8d76cd.png) [@Caleb\_Pearce](https://forum.asana.com/u/Caleb_Pearce)\
**Post date:** [28 June 2024 00:04 UTC](https://forum.asana.com/t/upload-attachment-using-python-sdk-v5/846077/3 "2024-06-28T00:04:28Z")

</div>

Thanks David,

I did get it working! The answer is obvious, once you know - I needed to use resource\_subtype = asana for a local file.

The attachment is automatically recognised as a PDF, I didn’t have to add any parameters.

The only change required in the file path is using double backslashes. Sometimes the uplaod works with single backslashes but I get a syntax warning, other times it doesn’t’ work at all.

Working code:

> import asana  
> from asana.rest import ApiException  
> from pprint import pprint
> 
> configuration = asana.Configuration()  
> configuration.access\_token = ‘my\_token’  
> api\_client = asana.ApiClient(configuration)
> 
> # create an instance of the API class
> 
> attachments\_api\_instance = asana.AttachmentsApi(api\_client)  
> opts = {  
> ‘resource\_subtype’: “asana”, # str |  
> ‘file’: “C:\Engineering\test.pdf”, # str |  
> ‘parent’: “my\_task\_gid”, # str |  
> #‘url’: “”, # str |  
> #‘name’: “test.pdf”, # str |  
> #‘connect\_to\_app’: True, # bool |  
> #‘opt\_fields’: “” #“connected\_to\_app,created\_at,download\_url,host,name,parent,parent.created\_by,parent.name,parent.resource\_subtype,permanent\_url,resource\_subtype,size,view\_url”, # list[str] | This endpoint returns a compact resource, which excludes some properties by default. To include those optional properties, set this query parameter to a comma-separated list of the properties you wish to include.  
> }
> 
> try:  
> # Upload an attachment  
> api\_response = attachments\_api\_instance.create\_attachment\_for\_object(opts)  
> pprint(api\_response)  
> except ApiException as e:  
> print(“Exception when calling AttachmentsApi-\>create\_attachment\_for\_object: %s\n” % e)

---

<div class="post-metadata">

**Author:** ![Phil\_Seeman](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/phil_seeman/32/81525_2.png) [@Phil\_Seeman](https://forum.asana.com/u/Phil_Seeman)\
**Post date:** [28 June 2024 00:16 UTC](https://forum.asana.com/t/upload-attachment-using-python-sdk-v5/846077/4 "2024-06-28T00:16:40Z")

</div>

Thanks, @Caleb_Pearce - as my fellow Forum Leader @Bastien_Siebman likes to say, there is a special place in heaven for those who come back and post working code for others to take advantage of in the future!

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/system/32/276370_2.png) [@system](https://forum.asana.com/u/system)\
**Post date:** [5 July 2024 00:16 UTC](https://forum.asana.com/t/upload-attachment-using-python-sdk-v5/846077/5 "2024-07-05T00:16:44Z")

</div>

Closed on 5 July 2024 00:16 UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
