Examples

On this page we have made som simple examples on how you can use the API in your programming language.

PHP Example

This simple examples shows how to list all contacts:

<?php
$method = "GET";
$resourceUrl = "https://api.billysbilling.dk/v1/contacts";
$apiKey = "6q7Eg1DtfVv7dM6Rnz9fVCzjeoEBRf38"; //Replace with your own API key. Get it via Billy's webinterface.
$c = curl_init($resourceUrl);
curl_setopt($c, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($c, CURLOPT_USERPWD, $apiKey . ":");
$rawResponse = curl_exec($c);
$response = json_decode($rawResponse);
print_r($response);

In practice you would make a BillyRequest class or the like to standardize the way you interface with the API. We hope to supply you with a PHP library that eases the use of in near future.

This other example shows how to create an invoice (the contact and product should be created first):

<?php
$method = "POST";
$resourceUrl = "https://api.billysbilling.dk/v1/invoices";
$requestData = array(
    "type" => "invoice",
    "contactId" => "256196-mmoqlYKyddZie",
    "attContactPersonId" => "256198-Uu43Cq8It3eYS",
    "contactMessage" => "Thank you for being a happy customer.",
    "entryDate" => "2012-03-20",
    "dueDate" => "2012-03-27",
    "currencyId" => "DKK",
    "lines" => array(
        array(
            "productId" => "256201-9iLgj9F6u8pPx",
            "quantity" => 2,
            "unitPrice" => 500,
            "discountText" => "Good customer discount",
            "discountMode" => "percent",
            "discountValue" => 25
        ),
        array(
            "productId" => "256201-9iLgj9F6u8pPx",
            "description" => "A lot of work",
            "quantity" => 1,
            "unitPrice" => 3900
        )
    )
));
$apiKey = "6q7Eg1DtfVv7dM6Rnz9fVCzjeoEBRf38"; //Replace with your own API key. Get it via Billy's webinterface.
$c = curl_init($resourceUrl);
curl_setopt($c, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($c, CURLOPT_POSTFIELDS, json_encode($requestData));
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($c, CURLOPT_USERPWD, $apiKey . ":");
$rawResponse = curl_exec($c);
$response = json_decode($rawResponse);
$invoiceId = $response->id; //The ID of the newly created invoice

PHP trouble shooting

If you get a curl error like Error setting certificate verify locations: CAfile: /etc/.../certs/ca-bundle.crt CApath: none it means that your local curl installation is not configured with an up-to-date SSL certificate bundle.

This can be fixed by downloading the file cacert.pem from http://curl.haxx.se/docs/caextract.html and then put the following code after your curl_init and before curl_exec:

curl_setopt ($ch, CURLOPT_CAINFO, "pathto/cacert.pem");

Ruby Example

This simple examples shows how to list all contacts:

require 'json'
require 'rest-client'
resource_url = 'api.billysbilling.dk/v1/contacts'
api_key = '6q7Eg1DtfVv7dM6Rnz9fVCzjeoEBRf38'
raw_response = RestClient.get 'https://' + api_key + ':@' + resource_url
response = JSON.parse(raw_response)
p response

This other example shows how to create an invoice (the contact and product should be created first):

resource_url = 'api.billysbilling.dk/v1/invoices'
request_data = {
  :type => 'invoice',
  :contactId => '256196-mmoqlYKyddZie',
  :attContactPersonId => '256198-Uu43Cq8It3eYS',
  :contactMessage => 'Thank you for being a happy customer.',
  :entryDate => '2012-03-20',
  :dueDate => '2012-03-27',
  :currencyId => 'DKK',
  :lines => {
    {
      :productId => '256201-9iLgj9F6u8pPx',
      :quantity => 2,
      :unitPrice => 500,
      :discountText => 'Good customer discount',
      :discountMode => 'percent',
      :discountValue => 25
    },
    {
      :productId => '256201-9iLgj9F6u8pPx',
      :description => 'A lot of work',
      :quantity => 1,
      :unitPrice => 3900
    }
  }
}
api_key = '6q7Eg1DtfVv7dM6Rnz9fVCzjeoEBRf38'
raw_response = RestClient.post 'https://' + api_key + ':@' + resource_url, request_data.to_json
response = JSON.parse(raw_response)
p response