๐Ÿ”Authentication

Ensuring Secure and Authenticated API Requests Using HMAC SHA256.

1. Signing an API Request

To ensure the security and integrity of your API requests, you need to sign your requests using HMAC SHA256.

This process involves creating a specific string from your request, and then generating a signature using your secret key.

Requirement: Request an API Key and Secret Key from Tyga Support at support@tygapay.com to access APIs.

2. Step-by-Step Guide

This guide provides a clear process for signing an API request, from converting a JSON body to a query string (handling nested fields with a dot .), constructing the string to sign, and finally signing it using HMAC SHA256.

This ensures your API requests are secure and authenticated.

2.1 Create the Query String from JSON

Depending on your programming language, use the following methods to convert a JSON object to a query string. Note that nested fields are handled using a dot (.).

const qs = require('qs');

const jsonObj = {
    field1: "value2",
    nestedField: {
        nestedField1: "nestedValue1"
    }
};

const queryString = qs.stringify(jsonObj, { encode: false, delimiter: '&', allowDots: true });
console.log(queryString); // Output: field1=value2&nestedField.nestedField1=nestedValue1

2.2 Construct the String to Sign

FULL URL:

https://api.com/users?test=xxx

API PATH:

/users?test=xxx

QUERYSTRING FROM BODY:

field1=value2&nestedField.nestedField1=nestedValue1

Construct the string to sign by concatenating the API PATH and the QUERYSTRING FROM BODY:

stringToSign = "/users?test=xxxfield1=value2&nestedField.nestedField1=nestedValue1"

2.3 Sign the String using HMAC SHA256

Use your programming language's libraries to sign the string using HMAC SHA256.

const crypto = require('crypto');
const secretKey = 'your-secret-key';
const stringToSign = '/users?test=xxxfield1=value2&nestedField.nestedField1=nestedValue1';

const signature = crypto.createHmac('sha256', secretKey)
                        .update(stringToSign)
                        .digest('hex');
console.log(signature);

Last updated