# Sending file with API

**URL:** <https://forum.asana.com/t/sending-file-with-api/16897>\
**Category:** API & Developers\
**Tags:** api, attachments\
**Created:** [28 February 2018 16:58 UTC](https://forum.asana.com/t/sending-file-with-api/16897 "2018-02-28T16:58:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cezar\_Ayran](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/cezar_ayran/32/28082_2.png) [@Cezar\_Ayran](https://forum.asana.com/u/Cezar_Ayran)\
**Post date:** [28 February 2018 16:58 UTC](https://forum.asana.com/t/sending-file-with-api/16897/1 "2018-02-28T16:58:23Z")

</div>

Hi there,

I have the code below:

```
	public function AsanaUpload($url){
		$url = 'https://app.asana.com/api/1.0/'.$url;
		
        $rsIntegration = $this->Integration($_COOKIE["idRefSite"], 1);
        $arIntegration = $rsIntegration->fetch(PDO::FETCH_OBJ);

        $headers = array('Authorization: Bearer '.$arIntegration->AsanaBearer.'');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_URL, $url);

            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->fields));
  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
}//AsanaUpload

```

And I’m calling the function:

```
                             $filename = realpath("test.jpeg");
                            $erp->fields['file'] = "@".$filename . ";";
                            $attach = $erp->AsanaUpload('tasks/NUMBER OF THE TASK/attachments');
                            print_r($attach);

```

Error 1:  
Array ( [errors] =\> Array ( [0] =\> Array ( [message] =\> file: File is not an object [help] =\> For more information on API status codes and how to handle them, read the docs on errors: [Build an app with Asana](https://asana.com/developers/documentation/getting-started/errors) ) ) )

And sometimes ERROR 2:  
Array ( [errors] =\> Array ( [0] =\> Array ( [message] =\> file: Missing input [help] =\> For more information on API status codes and how to handle them, read the docs on errors: [Build an app with Asana](https://asana.com/developers/documentation/getting-started/errors) ) ) )

I just went here [Build an app with Asana](https://asana.com/developers/api-reference/attachments#upload)

My question is: what am I missing? I’d like to use only the file path without sending the file via FORM but is not working… Please let me know if you can help me with. Thanks!!

---

<div class="post-metadata">

**Author:** ![Cezar\_Ayran](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/cezar_ayran/32/28082_2.png) [@Cezar\_Ayran](https://forum.asana.com/u/Cezar_Ayran)\
**Post date:** [28 February 2018 17:06 UTC](https://forum.asana.com/t/sending-file-with-api/16897/2 "2018-02-28T17:06:56Z")

</div>

I tried to create this header:

```
$headers = array('Authorization: Bearer '.$arIntegration->AsanaBearer.'', 'Content-Type: multipart/form-data; boundary=13242', 'User-Agent: Java/1.7.0_76', 'Host: localhost', 'Accept: */*', 'Connection: keep-alive', 'Content-Length: 141');

```

But when I run the code it’s takes a while and nothing happens or timeout…

---

<div class="post-metadata">

**Author:** ![Matt\_Bramlage](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/matt_bramlage/32/145_2.png) [@Matt\_Bramlage](https://forum.asana.com/u/Matt_Bramlage)\
**Post date:** [19 March 2018 19:16 UTC](https://forum.asana.com/t/sending-file-with-api/16897/3 "2018-03-19T19:16:30Z")

</div>

Hey there!

Attachments can be pretty tricky to get right, since they use a multipart form upload, and so are different than just a standard json payload like the rest of our API. I just double-checked that our [php client library](https://github.com/Asana/php-asana) makes this a little bit less painful; here’s a quick working example:

```
<?php
require dirname( __FILE__ ) . '/vendor/autoload.php'; // This is using Composer for 
dependencies
use Asana\Client;
$ASANA_PERSONAL_ACCESS_TOKEN = 
getenv('ASANA_PERSONAL_ACCESS_TOKEN'); // Get my access token from the 
shell environment
$ASANA_TEST_TASK_ID = getenv('ASANA_TEST_TASK'); // the ID of a task I use for 
testing
$client = Asana\Client::accessToken($ASANA_PERSONAL_ACCESS_TOKEN);

try {
  $file_data = file_get_contents("./pink-elephant.jpg");
  $file_name = "pink-elephant.jpg";
  $content_type = "image/jpeg";
  $attachment = $client->attachments->createOnTask($ASANA_TEST_TASK_ID, 
$file_data, $file_name, $content_type);
} catch (Asana\Errors\InvalidRequestError $e) {
  var_dump($e->response->body);
}

```

If you’re just using standard PHP curl, I believe that as [this StackOverflow question](https://stackoverflow.com/questions/21905942/posting-raw-image-data-as-multipart-form-data-in-curl) indicates the “modern” way of doing this is with the [CURLFile class](http://php.net/manual/en/class.curlfile.php), but I don’t have an example of that handy. Hopefully this will put you on the right path!

---

<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:** [30 May 2025 19:08 UTC](https://forum.asana.com/t/sending-file-with-api/16897/4 "2025-05-30T19:08:56Z")

</div>

Closed on 30 May 2025 19:08 UTC


