NAV Navbar
Bash PHP Java C# Python
  • Code Samples
  • Code Samples

    Request Signing

    #!/bin/bash
    SECRET="f90bcdc7-ab24-4f66-90ab-1835d703f19a"
    ENDPOINT="https://developer.paygenius.co.za/pg/api/v2/util/validate"
    PAYLOAD="{\"data\":\"value\"}"
    
    echo -en "$ENDPOINT\n$PAYLOAD" \
        | openssl sha256 -hmac "$SECRET" \
        | awk '{print $2}'
    
    <?php
    $endpoint = "https://developer.paygenius.co.za/pg/api/v2/util/validate";
    $payload = "{\"data\":\"value\"}";
    $secret = "f90bcdc7-ab24-4f66-90ab-1835d703f19a";
    
    echo hash_hmac('sha256', trim($endpoint."\n".$payload), $secret);
    
    import hmac
    import hashlib
    
    endpoint = 'https://developer.paygenius.co.za/pg/api/v2/util/validate'
    secret = 'f90bcdc7-ab24-4f66-90ab-1835d703f19a'
    payload = '{"data":"value"}'
    
    print hmac.new(secret, ('%s\n%s' % (endpoint, payload)).strip(), hashlib.sha256).hexdigest()
    
    import java.nio.charset.Charset;
    import javax.crypto.Mac;
    import javax.crypto.spec.SecretKeySpec;
    import org.apache.commons.codec.binary.Hex;
    
    public class PayGenius {
    
        public static void main(String[] args) throws Exception {
            String endpoint = "https://developer.paygenius.co.za/pg/api/v2/util/validate";
            String payload = "{\"data\":\"value\"}";
            String secret = "f90bcdc7-ab24-4f66-90ab-1835d703f19a";
    
            Mac hmac = Mac.getInstance("HmacSHA256");
            SecretKeySpec secretKeySpec = new SecretKeySpec(secret.getBytes(Charset.defaultCharset()), "HmacSHA256");
            hmac.init(secretKeySpec);
    
            String signature = new String(Hex.encodeHex(hmac.doFinal(String.format("%s\n%s", endpoint, payload).trim().getBytes(Charset.defaultCharset()))));
    
            System.out.println(signature);
        }
    }
    
    using System;
    using System.Text;
    using System.Security.Cryptography;
    
    class PayGenius
    {
        public static void Main (string[] args)
        {
            string endpoint = "https://developer.paygenius.co.za/pg/api/v2/util/validate";
            string payload = "{\"data\":\"value\"}";
            string secret = "f90bcdc7-ab24-4f66-90ab-1835d703f19a";
    
            HMACSHA256 hmac = new HMACSHA256 (Encoding.Default.GetBytes (secret));
    
            byte[] hash = hmac.ComputeHash (Encoding.Default.GetBytes (String.Format("{0}\n{1}", endpoint, payload).Trim()));
    
            string signature = String.Concat(Array.ConvertAll(hash, x => x.ToString("X2")));
    
            Console.Write (signature);
        }
    }
    

    The code samples on the right show how the request signature needs to be generated. The following information is used in this sample:

    Data Value
    Endpoint https://developer.paygenius.co.za/pg/api/v2/util/validate
    Secret f90bcdc7-ab24-4f66-90ab-1835d703f19a
    JSON Payload {"data":"value"}

    This data produces the following signature:
    8e2b0b84da61d92814eabd9e5e06d0178c27fa169c5f58d6478b22f89bf032e5

    Making Requests

    curl -X POST \
      https://developer.paygenius.co.za/pg/api/v2/util/validate \
      -H 'accept: application/json' \
      -H 'content-type: application/json' \
      -H 'x-signature: 8e2b0b84da61d92814eabd9e5e06d0178c27fa169c5f58d6478b22f89bf032e5' \
      -H 'x-token: b3394743-4c5b-496f-a0e6-06580ba12b1e' \
      -d '{"data":"value"}'
    
    <?php
    $endpoint = "https://developer.paygenius.co.za/pg/api/v2/util/validate";
    $payload = "{\"data\":\"value\"}";
    $signature = "8e2b0b84da61d92814eabd9e5e06d0178c27fa169c5f58d6478b22f89bf032e5";
    $token = "b3394743-4c5b-496f-a0e6-06580ba12b1e";
    
    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => $endpoint,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => $payload,
      CURLOPT_HTTPHEADER => array(
        "accept: application/json",
        "content-type: application/json",
        "x-signature: " . $signature,
        "x-token: " . $token
      ),
    ));
    
    $response = curl_exec($curl);
    
    curl_close($curl);
    
    echo $response;
    
    import requests
    
    endpoint = 'https://developer.paygenius.co.za/pg/api/v2/util/validate'
    payload = '{"data":"value"}'
    token = 'b3394743-4c5b-496f-a0e6-06580ba12b1e'
    signature = '8e2b0b84da61d92814eabd9e5e06d0178c27fa169c5f58d6478b22f89bf032e5'
    
    headers = {
        'x-signature': signature,
        'x-token': token,
        'content-type': "application/json",
        'accept': "application/json"
        }
    
    response = requests.request("POST", endpoint, data=payload, headers=headers)
    
    print(response.text)
    

    Continuing from the previous example, these code samples show how to make the api request with the generated signature.

    Data Value
    Endpoint https://developer.paygenius.co.za/pg/api/v2/util/validate
    JSON Payload {"data":"value"}
    Signature 8e2b0b84da61d92814eabd9e5e06d0178c27fa169c5f58d6478b22f89bf032e5
    Token b3394743-4c5b-496f-a0e6-06580ba12b1e