HMAC Authentication Method

HMAC Authentication Method

If you are starting to use Price2Spy or its REST API interface please follow the steps below. If you are an existing Price2Spy client who is already using our REST API and need access to the old documentation, please find it under ‘Old authentication method’.

 

You can authenticate HTTP requests by using the HMAC-SHA256 authentication scheme. (HMAC refers to hash-based message authentication code.) These requests must be transmitted over TLS.

Prerequisites

Provide each request with all HTTP headers required for authentication. The minimum required are:

Request headerDescription
HostInternet host and port number. (api.price2spy.com:443)
X-P2S-DateDate and time at which the request was originated. It can’t be more than 15 minutes off from the current Coordinated Universal Time (Greenwich Mean Time). The value is in UNIX Epoch format.
AuthorizationAuthentication information required by the HMAC-SHA256 scheme. Format and details are explained later in this article.

Example

Host: api.price2spy.com:443
X-P2S-Date: 1700417770
Authorization: HmacSHA256 {Client_ID}:{Signature}

For POST and PUT requests. If DELETE request has a body, it must be provided as well.

Request headerDescription
Content-TypeThis header must be "application/json"

Authorization header

Syntax

Authorization: HmacSHA256 {Client_ID}:{Signature}
ArgumentDescription
HmacSHA256Authorzation scheme (required).
Client IDYour Client ID. (required)
Signaturebase64 encoded HmacSHA256 of String-To-Sign. (required)

Client ID

Value of the client ID used to compute the signature.

Signature

Base64 encoded HmacSHA256 hash of the String-To-Sign. It uses the Client Secret identified by Client ID. base64_encode(HMACSHA256(String-To-Sign, Client_Secret))

String-to-Sign

It is a canonical representation of the request:

stringToSign=

HTTP_METHOD + ‘\n’ +

host_with_port + ‘\n’ +

content_type + ‘\n’ +

resource_path + ‘\n’ +

date_in_unix_epoch + ‘\n’ +

request_payload

ArgumentDescription
HTTP_METHODUppercase HTTP method name used with the request. For more information, see section 9.
host_with_portInternet host and port number. (api.price2spy.com:443)
content_typeThis header must be “application/json” for POST and PUT requests. If DELETE request has a body, it must be provided as well.
resource_pathConcatenation of request absolute URI path and query string. For more information, see section 3.3.
date_in_unix_epochThis header must be UNIX Epoch time and must be within 15 minutes of the current time. This date is included to prevent replay attacks.
request_payloadRequest body in JSON format. It must be provided even if there is no body. For requests without a body an empty string can be provided.

Generating the signature:

Example POST

stringToSign=
POST

api.price2spy.com:443

application/json

/rest/v1/get-products
1700485915

{“active”: true}

Example GET

stringToSign=
GET

api.price2spy.com:443

/rest/v1/get-brands
1700485915

“” Just an empty string for the payload, does not represent actual qoutation marks.

HMAC-related errors

{“message”:”Hmac signature mismatch”}

HMAC signature mismatch – The client-generated hash value and the server-generated hash value do not match.

{“message”:”Authorization header with HmacSHA256 scheme not provided”}

Provide a valid Authorization HTTP request header.

{“message”:”Hmac invalid timestamp header”}

Timestamp header not provided in the correct format.

{“message”:”Hmac missing timestamp header”}

Timestamp header not provided in the request.