# Add invoice\_items when creating new invoice

**URL:** <https://api-discuss.freeagent.com/t/add-invoice-items-when-creating-new-invoice/1142>\
**Category:** Documentation\
**Created:** [29 October 2018 22:06 UTC](https://api-discuss.freeagent.com/t/add-invoice-items-when-creating-new-invoice/1142 "2018-10-29T22:06:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![justinteractive](https://api-discuss.freeagent.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@justinteractive](https://api-discuss.freeagent.com/u/justinteractive)\
**Post date:** [29 October 2018 22:06 UTC](https://api-discuss.freeagent.com/t/add-invoice-items-when-creating-new-invoice/1142/1 "2018-10-29T22:06:03Z")

</div>

I’ve tried this every which way I can conjure up, but I cannot get the API to add a line item when creating a new invoice.

The below array build creates the new invoice without a problem but it won’t add the invoice\_items. Any suggestions or obvious mistakes?

$endpoint = ‘[https://api.freeagent.com/v2/invoices](https://api.freeagent.com/v2/invoices)’;  
$data[‘contact’] = ‘[https://api.freeagent.com/v2/contacts/](https://api.freeagent.com/v2/contacts/)’.$user-\>freeagent\_id;  
$data[‘dated\_on’] = date(‘Y-m-d’);  
$data[‘payment\_terms\_in\_days’] = 30;  
$data[‘invoice\_items’] = array(  
‘item\_type’ =\> ‘Services’,  
‘quantity’ =\> 1.0,  
‘description’ =\> 'Order ‘.$order-\>order\_id.’ - '.base\_url(‘orders/order/’.$order-\>order\_id),  
‘price’ =\> $order-\>order\_collector\_fee  
);

---

<div class="post-metadata">

**Author:** ![Andrew\_Stewart](https://api-discuss.freeagent.com/user_avatar/api-discuss.freeagent.com/andrew_stewart/32/149_2.png) [@Andrew\_Stewart](https://api-discuss.freeagent.com/u/Andrew_Stewart)\
**Post date:** [30 October 2018 09:40 UTC](https://api-discuss.freeagent.com/t/add-invoice-items-when-creating-new-invoice/1142/2 "2018-10-30T09:40:44Z")

</div>

I’m not familiar with PHP (is it PHP?) but I think the problem may be that `invoice_items` needs to be an array of hashes / dictionaries / associative arrays / whatever, whereas you have a single one.

---

<div class="post-metadata">

**Author:** ![Pat\_George](https://api-discuss.freeagent.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@Pat\_George](https://api-discuss.freeagent.com/u/Pat_George)\
**Post date:** [30 October 2018 10:27 UTC](https://api-discuss.freeagent.com/t/add-invoice-items-when-creating-new-invoice/1142/3 "2018-10-30T10:27:17Z")

</div>

What Andrew said.

Justin, if you can convert $data to json and post it here, we could get a better idea if it’s formatted correctly.

---

<div class="post-metadata">

**Author:** ![justinteractive](https://api-discuss.freeagent.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@justinteractive](https://api-discuss.freeagent.com/u/justinteractive)\
**Post date:** [30 October 2018 15:12 UTC](https://api-discuss.freeagent.com/t/add-invoice-items-when-creating-new-invoice/1142/4 "2018-10-30T15:12:17Z")

</div>

Thanks for the reply Andrew, yes I’ve tried both approaches for invoice\_items - no joy with either. Returning the data array, converting it to an object, json etc. but no luck

---

<div class="post-metadata">

**Author:** ![justinteractive](https://api-discuss.freeagent.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@justinteractive](https://api-discuss.freeagent.com/u/justinteractive)\
**Post date:** [30 October 2018 15:14 UTC](https://api-discuss.freeagent.com/t/add-invoice-items-when-creating-new-invoice/1142/5 "2018-10-30T15:14:23Z")

</div>

Hi Pat\_George,

Returned the data as a var dump and it’s telling me my invoice\_items is an array or arrays. Pulled it out as json below, think it’s semantically correct.

{  
“contact”:“[https://api.freeagent.com/v2/contacts/7388593](https://api.freeagent.com/v2/contacts/7388593)”,  
“dated\_on”:“2018-10-30”,  
“payment\_terms\_in\_days”:30,  
“net\_total”:“123.00”,  
“comments”:“Order 827 - [https://www.reyooz.com/orders/order/827](https://www.reyooz.com/orders/order/827) - Collector fee = 123”,  
“invoice\_items”:[  
{  
“position”:1,  
“item\_type”:“Services”,  
“quantity”:1,  
“description”:“Order 827 [https://www.reyooz.com/orders/order/827](https://www.reyooz.com/orders/order/827)”,  
“price”:“123”  
},  
{  
“position”:2,  
“item\_type”:“Services”,  
“quantity”:1,  
“description”:“Something to keep testing multiple invoice\_items”,  
“price”:200.01  
}  
]  
}

---

<div class="post-metadata">

**Author:** ![Pat\_George](https://api-discuss.freeagent.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@Pat\_George](https://api-discuss.freeagent.com/u/Pat_George)\
**Post date:** [31 October 2018 11:33 UTC](https://api-discuss.freeagent.com/t/add-invoice-items-when-creating-new-invoice/1142/6 "2018-10-31T11:33:38Z")

</div>

Hi, Justin. You will want to make sure the entire variable is wrapped in an “invoice” key. Like so:

{“invoice”:  
{  
“contact”:“[https://api.freeagent.com/v2/contacts/7388593”](https://api.freeagent.com/v2/contacts/7388593%E2%80%9D),  
“dated\_on”:“2018-10-30”,  
“payment\_terms\_in\_days”:30,  
“net\_total”:“123.00”,  
“comments”:“Order 827 - [https://www.reyooz.com/orders/order/827](https://www.reyooz.com/orders/order/827) - Collector fee = 123”,  
“invoice\_items”:[  
{  
“position”:1,  
“item\_type”:“Services”,  
“quantity”:1,  
“description”:“Order 827 [https://www.reyooz.com/orders/order/827”](https://www.reyooz.com/orders/order/827%E2%80%9D),  
“price”:“123”  
},  
{  
“position”:2,  
“item\_type”:“Services”,  
“quantity”:1,  
“description”:“Something to keep testing multiple invoice\_items”,  
“price”:200.01  
}  
]  
}  
}

I did reproduce your problem and wrapping it fixed it. I’ll make an internal card to either handle it as you’d expect with and without the wrapper or make sure the documentation is specific about the wrapper requirement.

Thanks for bringing it up and sorry about the problems it caused.
