Authentication
This document describes the authentication scheme used in API endpoints that require it.
The authentication scheme used is based on HMAC, and follows a scheme similar to that of AWS.
You will get your ACCESS_ID
and SECRET_KEY
from your point of contact at Balance.
How it works
1. Create a canonical string for your request:
Details:
http_method
is an upper case version of the full HTTP method. Supported methods for this API are:GET, POST, PUT, PATCH, DELETE
content_type
is a copy of theContent-Type
header. For this API, set this toapplication/json
.request_uri
is the URI of the request. Do not include query params. For example:/api/v1/wallets
data_hash
is a SHA-256 hash of the request body, encoded as a HEX string. If the body field is empty, leave this field as an empty string. For example, if the body is the following string:{"name": "foobar"}
, thedata_hash
field should be set to:e684679449a32cb2477110ce15b02eace29dbfc89b9f8597a90d5702d5f60695
timestamp
is a UNIX timestamp equivalent of theDate
header. E.g.1561661184
(equivalent toThu, 27 Jun 2019 18:46:24 GMT
). Note that if this date is more than 15 minutes behind or ahead of the server's current date, the request will be rejected.
2. Create a signature
The signature is a SHA-256 based HMAC signature, keyed on the SECRET_KEY
provided to you by Balance. The signature must be HEX-encoded.
An example of this code in Ruby would be:
3. Add headers to your request
Your request now requires the following headers:
User-Agent
- must be included in all requests; choose a value that helps identify your integration.Content-Type
- this header should always be set toapplication/json
for this APIDate
- current date (see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Date)Authorization
- should be constructed in the following way:BalanceAPIAuth #{ACCESS_ID}:#{signature}
Only Content-Type
, Date
, and Authorization
are used in the signature generation. User-Agent
is required, but not part of the signed headers.
Examples
For each example, assume that your authentication credentials are:
And the timestamp is the following:
which is equivalent to a UNIX epoch timestamp of:
POST request with data:
The following is a valid curl POST request:
Note that this should produce the following canonical_string
:
GET request without data:
The following is a valid curl GET request:
Note that this should produce the following canonical_string
:
Helper Libraries
Ruby
There's a helper module packaged together with this doc in hmac_auth_helpers.rb
(see below). You can inspect the Ruby code to help you understand how to implement this scheme in your language of choice.
You can also use it to generate a curl
request for testing purposes. Here's an example (note that access_id
and secret_key
in this example are dummy values):
You can then copy the above curl
command and paste it in your shell. This request will be valid for 15 minutes, provided that you specified correct parameters.
Postman
In addition to the Ruby helper, you can use the following JavaScript snippet in Postman to automate HMAC authentication.
Before running requests, make sure your Postman Environment includes the following variables:
accessId
– your API access ID from BalancesecretKey
– your API secret key from BalanceuserAgent
– a custom string to help identify your integration (e.g., my-client-prod)
Paste the contents of postman_helper.js
into the Pre-request Script tab of your Postman request or collection
Make sure to include the following headers in your request: Content-Type
, Authorization
, Date
, and User-Agent
. These should be added to the Headers tab in Postman using the environment variables defined by the pre-request script:
Last updated