Skip to content
Last updated

CreateInvoice

Create an invoice using the provided parameters.

Request Parameters

ParameterTypeDescriptionRequired
actionstring"CreateInvoice"Required
useridintThe ID of the client to create the invoice for.Required
statusstringThe status of the invoice being created. Defaults to unpaid.Optional
draftboolWhether to create the invoice in draft status. (You do not need to pass $status with this.)Optional
sendinvoiceboolWhether to send the Invoice Created Email to the client. (You can't use this with $draft.)Optional
paymentmethodstringThe payment method of the created invoice, in system format.Optional
taxratefloatThe first-level tax rate to apply to the invoice to override the system default.Optional
taxrate2floatThe second-level tax rate to apply to the invoice to override the system default.Optional
datestringThe creation date that the invoice should display. Format: YYYY-mm-ddOptional
duedatestringThe due date of the newly-created invoice. Format: YYYY-mm-ddOptional
notesstringThe notes to appear on the created invoice.Optional
itemdescriptionxstringThe line item's description. X is an integer to add multiple invoice items.Optional
itemamountxfloatThe line item's amount.Optional
itemtaxedxboolThe line item's taxed value.Optional
autoapplycreditboolWhether to automatically apply credit from the client account to the invoice.Optional

Response Parameters

ParameterTypeDescription
resultstringThe result of the operation: success or error
invoiceidintThe ID of the newly-created invoice.
statusstringThe status of the newly-created invoice.

Example Request (CURL)

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.example.com/includes/api.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
    http_build_query(
        array(
            'action' => 'CreateInvoice',
            // See https://developers.whmcs.com/api/authentication
            'username' => 'IDENTIFIER_OR_ADMIN_USERNAME',
            'password' => 'SECRET_OR_HASHED_PASSWORD',
            'userid' => '1',
            'status' => 'Unpaid',
            'sendinvoice' => '1',
            'paymentmethod' => 'mailin',
            'taxrate' => '10.00',
            'date' => '2016-01-01',
            'duedate' => '2016-01-08',
            'itemdescription1' => 'Sample Invoice Item',
            'itemamount1' => '15.95',
            'itemtaxed1' => '0',
            'itemdescription2' => 'Sample Second Invoice Item',
            'itemamount2' => '1.00',
            'itemtaxed2' => '1',
            'autoapplycredit' => '0',
            'responsetype' => 'json',
        )
    )
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);

Example Request (Local API)

$command = 'CreateInvoice';
$postData = array(
    'userid' => '1',
    'status' => 'Unpaid',
    'sendinvoice' => '1',
    'paymentmethod' => 'mailin',
    'taxrate' => '10.00',
    'date' => '2016-01-01',
    'duedate' => '2016-01-08',
    'itemdescription1' => 'Sample Invoice Item',
    'itemamount1' => '15.95',
    'itemtaxed1' => '0',
    'itemdescription2' => 'Sample Second Invoice Item',
    'itemamount2' => '1.00',
    'itemtaxed2' => '1',
    'autoapplycredit' => '0',
);
$adminUsername = 'ADMIN_USERNAME'; // Optional for WHMCS 7.2 and later

$results = localAPI($command, $postData, $adminUsername);
print_r($results);

Example Response JSON

{
    "result": "success",
    "invoiceid": "1",
    "status": "Unpaid"
}

Error Responses

Possible error condition responses include:

  • Client ID Not Found
  • Cannot create and send a draft invoice in a single API request. Please create and send separately.

Version History

VersionChangelog
1.0Initial Version