Getting account balance via the API

39 views
Skip to first unread message

Chris Rockwell

unread,
May 23, 2024, 6:47:46 PMMay 23
to Bkper
First off, I'm _very_ new to Bkper and GCP.  Please accept advance apologies if I mess up the terminology.  My goal is to retrieve the account balance (not sure if this is called a book, or an account, but I just need to know the total at a given time).

I am assuming we can use the API to do that but I'm a bit confused on the best way to authenticate.  As of now, I'm looking at the service account flow - would that be the best way if this is to be consumed via a cron job on a separate linux server?  If it helps, or matters, this will be a PHP application that, for example, will retrieve the balance nightly.

Thanks for any help!

Mael Caldas

unread,
May 24, 2024, 8:29:50 AMMay 24
to Bkper
Hi Chris,

Yes, you can use the API to do that, although, you should use a token that identifies an user account, instead of a service account. 

You can do a server side authentication, with offline access, and keep an refresh_token on your side, then refreshing the access_token when expired, calling it from a cron job, with no user context. There is an PHP library that helps on that.

The fetch the balances, you should use the books.balances.list endpoint and send a query to filter an account or group, as well as the dates, the same way you do when click in an account in the dashboard, for example: group:'Total Equity' on:01/2028

I hope that helps.

Cheers,
Mael

Chris Rockwell

unread,
Jun 18, 2024, 1:50:23 PMJun 18
to Bkper
Hi Mael - thank you very much for the response.  Using the linked github repo I am now able to get an access_token and refresh_token.

The next step is to actually query the Bkper API but I'm getting a 401 error with the message: "401 Method doesn't allow callers without established identity. Please use an API key or other form of API consumer identity to call this API.".  I get the same error when trying to use the API Explorer.  I've done a few iterations, but I'm currently creating a request like this:

        $filter = "group:'Current Assets' after:01/2023 before:07/2024";
        $path = "https://bkper-hrd.appspot.com/_ah/api/bkper/v5/books/[book id redacted]/balances";
        $request = Http::get($path, [
            'headers' => [
                'Authorization' => 'Bearer [access token retrieved via oauth redacted]'
            ],
            'query' => [
                'query' => $filter,
                'key' => '[key via google dashboard]',
            ],
        ]);
        $response = $request->body();

I'm hoping someone can spot what I'm doing incorrectly here.  Thank you!

Chris Rockwell

unread,
Jun 18, 2024, 2:16:59 PMJun 18
to Bkper
I might be slightly closer now.  I am getting this error: 500 Request blocked due to unsupported block reason Requests from referer [my-app-url-redacted.com] are blocked.

I get this after refactoring my request code like this:


$filter = "group:'Current Assets' after:01/2023 before:07/2024";
        $path = "https://app.bkper.com/_ah/api/bkper/v5/books/[redacted]/balances";
        $queryParams = http_build_query([
            'query' => $filter,
            'key' => '[redacted]'
        ]);
        $fullUrl = $path . '?' . $queryParams;

        $request = Http::withHeaders([
            'Authorization' => 'Bearer [redacted]',
            'referer' => '[redacted].com',
        ])->get($fullUrl);

        $response = $request->body();

Chris Rockwell

unread,
Jun 18, 2024, 3:15:56 PMJun 18
to Bkper
Ok, I'm all set.  I only needed the admin to add the application URL as a referer and I'm getting the data.  Thanks much!

Mael Caldas

unread,
Jun 19, 2024, 8:34:13 AMJun 19
to Bkper
Great! You've found the key param that was missing. 

Glad to know its working now :-)

Cheers,
Mael
Reply all
Reply to author
Forward
0 new messages