John Davidson

php - Xero - Creating Multiple invoices via API

0 comments
Message:


In my Laravel/VueJS system I have a routine that i called to create invoices from our system into Xero.


I've a two fold issue, one is that when I have 300+ invoices, I get a 504 timeout error BUT all the invoices seem to transfer and are created.


The second is that I sometimes get a Xero API timeout as I've called it too many times in a 60 second period.


What I do at the moment is:-


Read my invoices
Create a new invoice
Call Xero and find the contacts ContactID from Xero
Pause a second
Add all the data to the new invoice
Save the new invoice to Xero
Wait a second
Repeat until the end

But, is there a way I can do the following:-


Read my invoices
Create a new invoice
Call Xero and find the contacts ContactID from Xero
Pause a second
Add all the data to the new invoice
Wait a second
Repeat until the end
Save all the invoices to Xero

Therefore, is it possible to create a variable called $batchInvoices, store each invoice in that variable, then push $batchInvoices to Xero?


Any help would be gratefully appreciated.


Carl.



My Answer:
Yes, it is possible to batch create multiple invoices in Xero using their API. Instead of creating and saving each invoice individually, you can store all the invoices in an array or collection and then send them to Xero in a single API call.

Here is an example of how you can batch create invoices in Xero using PHP:

php
// Read your invoices and store them in an array
$invoices = [
// Invoice 1 data
// Invoice 2 data
// ...
];

// Create an array to store the invoices to be sent to Xero
$batchInvoices = [];

foreach ($invoices as $invoiceData) {
// Create a new invoice object and add data
$newInvoice = new \XeroPHP\Models\Accounting\Invoice();
// Add invoice data to $newInvoice

// Add the new invoice to the batchInvoices array
$batchInvoices[] = $newInvoice;
}

// Send the batchInvoices array to Xero in a single API call
$createdInvoices = $xero->accountingApi()->createInvoices($batchInvoices);

// Handle the response from Xero
foreach ($createdInvoices as $createdInvoice) {
// Handle the response for each invoice
}


By batching the invoices and sending them in a single API call, you can reduce the number of API calls made to Xero and potentially avoid the timeout issues you are experiencing. Make sure to check the Xero API documentation for any specific requirements or limitations when batch creating invoices.

Rate this post

3 of 5 based on 5097 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us