Go Example for Calling API
Golang Example for Calling the CatFee.IO Rest API
Prerequisites
You need a valid API Key and API Secret.
Make sure your Go environment has access to standard libraries such as net/http
and crypto/hmac
.
Sample Code
Code Explanation
generateTimestamp()
generateTimestamp()
Generates the current UTC time formatted as an ISO 8601 timestamp using Go’s time
package.
buildRequestPath()
buildRequestPath()
Takes a map of query parameters and constructs a full query string appended to the request path. Parameters are joined with &
.
generateSignature()
generateSignature()
Creates a signature string by concatenating timestamp + method + requestPath
, and then signs it using HMAC-SHA256 with the API Secret. The result is Base64 encoded.
createRequest()
createRequest()
Builds the HTTP request with appropriate headers (CF-ACCESS-KEY
, CF-ACCESS-SIGN
, CF-ACCESS-TIMESTAMP
) and sends it using the standard http
package.
main()
main()
Defines request method and query parameters, generates timestamp, request path, and signature, builds the final URL, and sends the request. Finally, the response is read and printed.
Notes
API Key and Secret
Make sure to replace APIKey
and APISecret
with your actual credentials from CatFee.IO.
Query Parameter Order
This example concatenates query parameters directly without sorting. If your application requires sorted parameters, be sure to implement sorting.
Response Handling
The response is read using ioutil.ReadAll()
. If the response body is in JSON format, consider using the encoding/json
package to parse the response.
HTTP Methods
This example supports POST
, but you can change it to GET
, PUT
, or DELETE
depending on your needs.
Summary
This example demonstrates how to securely call the CatFee.IO Rest API using Go, including HMAC-SHA256 signature generation for authentication. You can adjust the code to use different HTTP methods and handle various API responses accordingly.
Last updated
Was this helpful?