Java Example for Calling API
Java Example for Calling the CatFee.IO REST API
Prerequisites
You must have a valid API Key and API Secret.
Ensure your environment has Java 8 or higher installed and includes the following dependencies:
Example Code
Code Explanation
method
Variable: Themethod
in themain()
function is defined as"POST"
, which can be changed to"GET"
,"PUT"
, or"DELETE"
as needed.Generate Current Timestamp: The
generateTimestamp()
function returns the current UTC time in ISO 8601 format usingInstant.now().toString()
.Build Request Path: The
buildRequestPath()
function takes query parameters and appends them to the path to form the full request path.Generate Signature: The
generateSignature()
function creates the string to be signed by concatenatingtimestamp + method + requestPath
, and uses thehmacSHA256()
method with the API Secret to generate the signature.HMAC-SHA256 Signing: The
hmacSHA256()
function implements HMAC-SHA256 signing and returns a Base64-encoded result.Create Request: The
createRequest()
function dynamically builds the HTTP request based on the specified method (GET, POST, PUT, DELETE). For POST and PUT requests, it usesHttpRequest.BodyPublishers.noBody()
.Send Request and Handle Response: It uses
HttpClient
to send the HTTP request and prints the response status and body. If the response status is not 200, it logs an error.
Notes
API Key & Secret: Make sure to replace
API_KEY
andAPI_SECRET
with the actual values obtained from CatFee.IO.Purpose of the Signature: The signature ensures the integrity and authenticity of the request. It is generated using the combination of timestamp, HTTP method, request path, and query parameters.
Query Parameter Order: There is no need to sort query parameters. Use them in their original order.
HTTP Methods: The sample supports POST, PUT, GET, and DELETE methods. All parameters are passed through the URL query string.
Handling Response: The sample prints the response directly. You can parse the body as JSON or other formats for further processing.
Summary
This example demonstrates how to securely interact with CatFee.IO Rest API in Java. By using HMAC-SHA256 signatures and proper query handling, it ensures each request is valid and protected. You can customize this code to perform different operations and HTTP methods as needed.
For any questions or further assistance, feel free to contact the CatFee.IO support team.
Last updated
Was this helpful?