# Unable to add custom fields while creating new task in asana API using PHP

**URL:** <https://forum.asana.com/t/unable-to-add-custom-fields-while-creating-new-task-in-asana-api-using-php/77987>\
**Category:** API & Developers\
**Created:** [22 March 2020 05:25 UTC](https://forum.asana.com/t/unable-to-add-custom-fields-while-creating-new-task-in-asana-api-using-php/77987 "2020-03-22T05:25:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon97086195](https://luna1.co/8d76cd.png) [@anon97086195](https://forum.asana.com/u/anon97086195)\
**Post date:** [22 March 2020 05:25 UTC](https://forum.asana.com/t/unable-to-add-custom-fields-while-creating-new-task-in-asana-api-using-php/77987/1 "2020-03-22T05:25:40Z")

</div>

Hi,

I am trying to add custom fields in asana task through API using PHP, I have followed the JSON format as per the website: [Build an app with Asana](https://developers.asana.com/docs/#create-a-task). I have attached my code to this post. Could someone please help me? I am not getting any error but it’s not creating task.

```
$api = 'xxxxxxxxxxxxxxxxx';
$url = 'https://app.asana.com/api/1.0/tasks';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Don't print the result
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // Don't verify SSL connection
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // "" ""
curl_setopt($curl, CURLOPT_USERPWD, $api);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));

$cust = array(
    "mantis_due_date" => "2020-02-03",
    "mantis_url" => "This is some text"
  );
$data = array(
    "data" => array(
	
        "workspace" => "xxxxxxxxxxxxxx",
        "projects" => array('xxxxxxxxxxxx'),
		"custom_fields" => $cust,
        "name" => "add custom field",
        "notes" => "notes1",
        "assignee" => "xxxxxxxxxxx",
		"due_on"=> "2020-03-02"		
 )   
);
$data = json_encode($data);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);        
$html = curl_exec($curl);   
curl_close($curl);

$html = json_decode($html);
var_dump($html);

```

Thank you!

---

<div class="post-metadata">

**Author:** ![Bastien\_Siebman](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/bastien_siebman/32/172354_2.png) [@Bastien\_Siebman](https://forum.asana.com/u/Bastien_Siebman)\
**Post date:** [22 March 2020 08:05 UTC](https://forum.asana.com/t/unable-to-add-custom-fields-while-creating-new-task-in-asana-api-using-php/77987/2 "2020-03-22T08:05:47Z")

</div>

I am wondering if you can add the fields at the same time as creating the task. @Phil_Seeman am i right?

---

<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:** [23 March 2020 03:23 UTC](https://forum.asana.com/t/unable-to-add-custom-fields-while-creating-new-task-in-asana-api-using-php/77987/3 "2020-03-23T03:23:52Z")

</div>

I’m not familiar with PHP so I can’t comment on the correctness of the specific syntax, but to @Bastien_Siebman’s comment, that could be the problem. Are those two custom fields already attached to the project? To add a custom field to a task, you first need to:

1. [Create the custom field](https://developers.asana.com/docs/#create-a-custom-field) if it doesn’t currently exist.
2. [Add the custom field to the project](https://developers.asana.com/docs/#add-a-custom-field-to-a-project) if it’s not already attached to the project.

Also, I would try the call without the custom field portion and see if that works.

---

<div class="post-metadata">

**Author:** ![anon97086195](https://luna1.co/8d76cd.png) [@anon97086195](https://forum.asana.com/u/anon97086195)\
**Post date:** [23 March 2020 17:36 UTC](https://forum.asana.com/t/unable-to-add-custom-fields-while-creating-new-task-in-asana-api-using-php/77987/4 "2020-03-23T17:36:56Z")

</div>

@Phil_Seeman, yes it’s working now after creating custom fields in project

---

<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:07 UTC](https://forum.asana.com/t/unable-to-add-custom-fields-while-creating-new-task-in-asana-api-using-php/77987/5 "2025-05-30T19:07:03Z")

</div>


