Is there any sample PHP code for the API...


that might be a very lazy question but I’m still asking because you probably have some code lying around.

I need to do some simple API call to finish my billing integration.

  • Create&Update a customer and retreive the cusID for further purpose
  • Create an Invoice and apply a payment
  • List customer invoices and get a print link.

If you have something to get me started it would be nice.

I’ll post my code afterwards. i have nothing to hide.


You can reference our test class get an idea for using our API with PHP code, the main function is sendRequest().

Hi Hillel,

I’ve tried a lot of stuff that resulted in various errors…

A few questions :

  1. What’s the difference between the Token and the API secret? In the code you gave me, the method _before() needs an API secret to get the TOKEN. Now, maybe I’m blind but I do have a token available in Advanced Settings / API Token but I don’t have both.

  2. Here’s a little code sample. I’m just trying to get a list of invoices for customers #3 which does exists.

function sendRequest($url, $data, $type = 'POST')
        $url = "" . $url;
        echo $url;
        $data = json_encode($data);
        echo $data;
        $curl = curl_init();
        $opts = [
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CUSTOMREQUEST => $type,
            CURLOPT_POST => $type === 'POST' ? 1 : 0,
            CURLOPT_POSTFIELDS => $data,
            CURLOPT_HTTPHEADER  => [
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data),
                'X-Ninja-Token: yCypjpCtoTjmZwpKMdnw5U7RYH8s1xJp',  // Token I created in advanced settings
        curl_setopt_array($curl, $opts);
        $response = curl_exec($curl);
        echo $response;
        return json_decode($response);

$data = new stdClass;
sendRequest('invoices/3', $data);

It returns «Whoops, looks like something went wrong.» with some layout and CSS…

If I would request :
sendRequest(‘invoices’, $data);

It actually redirects me to the billing system.

  1. Is it because the API system is not available on Self Host? Or because I didn’t yet pay for the White Label? Because I definitely plan to do so once I finish this integration.

Thanks :slight_smile: I’m getting there.

OKay, forget about #2 and #3, I figured it out.

On your site it says :

To filter by client add the clientId to the URL. curl -X GET -H "X-Ninja-Token: TOKEN" -H "X-Requested-With: XMLHttpRequest"

But I got the results I want by trying :

   $data = new stdClass;
    sendRequest('invoices', $data, "GET");

I see that there’s an API for emailing_invoice but is there one that return a link to the pdf?

OK, I wish I could just start all over. It’s a mess and I can’t edit.

I’m now able to insert/edit clients.

A few questions :

  1. I edited the tax_rates field in the database to allow 3 number after the dot. It’s working perfectly. Will it get updated or I’m okay?

  2. I see there’s a table affiliates in the database. Is there some hidden feature I can activate and help you develop?

Sorry for the trouble, I’ve corrected the docs. We used to support /$clientId but have switched to ?client_id=$clientId to be more RESTful.

  1. It’s possible the code will be changed in the future which may break this, it’s hard to predict as it isn’t officially supported.

  2. The affiliates table is used by our main site to track sales (ie, white label licenses). If you’re interested in helping develop the app we’re always accepting pull requests, and if you’d like to earn money telling people about the app check out our referral program.

Hi Hillel, thanks a lot for the answer…

As for the point 2), I’ll certainly recommends your software. I think it’s doing everything well and the fact that you’re actively developping it is a major selling point.

I’ll assuredly participate in the codebase like i’m doing in all open source project i’m using.

As for the API, I have another question (for now at least)…

I am trying to fetch a single customer information WITH the contact info as well since the email is only kept there.

I’ve tried the API “clients” with GET but it always return me a list of customers with no contact:

$data = new stdClass;
$data->public_id=26; // Also tried with id and client_id
sendRequest('clients', $data, "GET");

Also tried the same thing with POST to see if it would return unmodified data but it “Whoops” me. In laravel.log, I have :

[2016-01-27 17:21:03] production.ERROR: exception ‘ErrorException’ with message ‘Undefined index: contacts’ in /home/facturation/app/Ninja/Repositories/ClientRepository.php:85
Stack trace:
#0 /home/facturation/app/Ninja/Repositories/ClientRepository.php(85): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, ‘Undefined index…’, ‘/home/facturati…’, 85, Array)

What’s the best way to get the full customer info?

Thanks :slight_smile:

You should be able to load the contacts by adding ?include=contacts to the URL.

Oh yeah it works! Thanks and for fetching a single customer?

There’s surely a way to not go through cursor pagination?

In the next version you’re able filter by email address.

But not by ID?

You can filter other objects by client id but not clients themselves, we’ll look into adding this in the future.

Has this been implemented yet ?
I try to interface a help desk / ticket system with Invoice Ninja and need to be able to create an new user/contact (that works!) But also verify if there already is an account by that emailadres or user id.
I could perhaps help to add this functionality if this has not been implemented due to time constrains.

@Arend, this functionality is only supported for resellers, email us at to discuss further.