Forum

Ask, reply and learn. Join the community of Akaunting.

New Discussion

How to use REST API

Mathieu Ici   ( User )

Commented 3 years ago

Hi,

I have been trying to figure out how to use the REST API without success. Here are a few questions:

1- Using the cloud version, which URL must I use for authentication? (is this documented somewhere?)

2- How do I provide the API key using the following code sample:

$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "???");
curl_setopt($curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl_handle, CURLOPT_USERPWD, "[user:password]");
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$curl_data = curl_exec($curl_handle);
curl_close($curl_handle);

3- Are there any basic examples on how to use the REST API somewhere? For example:

• PHP code to authenticate
• PHP code to read clients list
• PHP code to create an invoice

Just those 3 examples would be enough to get going :-)


Thanks x 1000!

Stojan   ( User )

Commented 3 years ago

For which purpose you need to? Which framework you use or you plan to make it in basic PHP?

Mathieu Ici   ( User )

Commented 3 years ago

No framework, simply to write my own code to interface Akaunting with an existing project.

Stojan   ( User )

Commented 3 years ago

Authentication route does not exist and you do not need it. You can use Basic Authentication where you can send email/password. For test login info you can point to https://app.akaunting.com/api/ping. Here is a list of API routes that you can use:
https://github.com/akaunting/akaunting/blob/master/routes/api.php

Mathieu Ici   ( User )

Commented 3 years ago

Thanks for those helpful pointers! I am slowly making progress and installed Akaunting on my own server.

Now, making a call to...
https://myserver.com/api/companies
...works like expected.

But, same thing to ...
https://myserver.com/api/contacts
...gives me the error message "User does not have any of the necessary access rights."

Both are using my admin account that has all permissions. Am I missing something?

Stojan   ( User )

Commented 3 years ago

I am preparing a Postman collection with will be soon added to the project so you can use it.

Mathieu Ici   ( User )

Commented 3 years ago

That would be awesome! Saving a lot of time and headaches for people trying to figure out how to use the REST API.

I finally found out I needed to pass the company ID and contact type to get results from my contacts query, i.e. "api/contacts?company_id=1&search=type:customer".

Now I'm trying to create a customer ;-)

Posting this JSON payload...

{
"company_id":1,
"type":"customer",
"name":"John Doe",
"email":"[email protected]",
"tax_number":null,
"phone":"123-456-7890",
"address":"Here",
"website":null,
"currency_code":"EUR",
"enabled":true,
"reference":null
}

to "...api/contacts" gives me the "User does not have any of the necessary access rights" error, which I've come to understand is not related to access rights but that something else went wrong.

My HTTP header sets the content-type to application/json.

Is a required parameter missing or should the POST be using form-data or x-www-form-urlencoded instead of JSON?

Thanks again

Laurent Rime   ( User )

Commented 3 years ago

Thank you @M for posting this question. I am having exactly the same issues as you, trying to reverse engineer the different routes and parameters is pretty tedious.
Do you by any chance have found a way to get invoices? Reading the forum it seems like it used to exist a /invoices route but I cannot find any in the code in version 2.1.1.

Thank you Stojan for your help in creating a postman collection. Could you already put something together?

Mathieu Ici   ( User )

Commented 3 years ago

Hi @Lolo Max,

Yes, I managed to find how to get contacts and invoices.

To get invoices this GET request works:

https://example.com/api/documents?company_id=1&search=type:invoice

I have not figured the parameters to filter invoices (ex: by ID or email) nor how to use pagination.

I spent a lot of time trying to figure out how to create contacts and invoices, without success :-( I'm longing for some developer documentation, hopefully @Stojan will have his Postman collection up soon.

Laurent Rime   ( User )

Commented 3 years ago

Hi @M,

Thanks for your input. Indeed, I could make the invoice route work.

Let's see when the holy grail Postman collection will arrive. It will be of great help indeed!

Cheers

Please login or register to leave a response.

Showing 1 to 10 of 62 discussions