Authentication Code Examples

Below are very simple examples of secret key signature authentication implementations of authentication in a couple of different languages.

Each example is annotated with todo notes that you must address before the code is usable. Examples contain at least an HTTP JSON API call with a signature against the Wyre production API (the testwyre.com environment URI is commented out but included).

Feel free to also check out our Postman collection here for a suite of requests to all of our endpoints.

<?php
    function make_authenticated_request($endpoint, $method, $body) {
        // $url = 'https://api.testwyre.com'; // todo use this endpoint for testwyre environment
        $url = 'https://api.sendwyre.com';
      
        // todo please replace these with your own keys for the correct environment
        $api_key = "AK-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA";
        $secret_key = "SK-AAAAAAA-AAAAAAA-AAAAAAA-AAAAAAA";

        $timestamp = floor(microtime(true)*1000);
        $request_url = $url . $endpoint;

        if(strpos($request_url,"?"))
            $request_url .= '&timestamp=' . sprintf("%d", $timestamp);
        else
            $request_url .= '?timestamp=' . sprintf("%d", $timestamp);

        if(!empty($body))
            $body = json_encode($body, JSON_FORCE_OBJECT);
        else
            $body = '';

        $headers = array(
            "Content-Type: application/json",
            "X-Api-Key: ". $api_key,
            "X-Api-Signature: ". calc_auth_sig_hash($secret_key, $request_url . $body)
        );
        $curl = curl_init();

        if($method=="POST"){
          $options = array(
            CURLOPT_URL             => $request_url,
            CURLOPT_POST            =>  true,
            CURLOPT_POSTFIELDS      => $body,
            CURLOPT_RETURNTRANSFER  => true);
        }else {
          $options = array(
            CURLOPT_URL             => $request_url,
            CURLOPT_RETURNTRANSFER  => true);
        }
        curl_setopt_array($curl, $options);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        $result = curl_exec($curl);
        curl_close($curl);
        var_dump($result);
        return json_decode($result, true);
    }

    function calc_auth_sig_hash($seckey, $val) {
        $hash = hash_hmac('sha256', $val, $seckey);
        return $hash;
    }

    // try to retrieve the current account
    echo make_authenticated_request("/v2/account", "GET", array());

    // perform a transfer
    $transfer = array(
      "sourceCurrency"=>"USD",
      "dest"=>"account:AC-XXXXXXXX", // replace with your account ID
      "sourceAmount"=> "100.50",
      "destCurrency"=> "BTC",
      "message"=> "convert USD to bitcoin"
      );
    echo make_authenticated_request("/v2/transfers", "POST", $transfer);
?>

Last updated

Was this helpful?