Getting Started with ThirdFi, a Developer Guide

#BuidlWithThirdFi, Learn
what is ThirdFi ?

Compared to traditional financial systems, DeFi solves many problems in the current system. But DeFi is not easy and building a DeFi app from scratch will take months of development and rigorous testing. As a result of these issues, companies and users suffer heavy losses in the case of a malfunction. 

Here comes ThirdFi, which provides unified framework to build DeFi apps on it. ThirdFi provides a middleware to build products on top of it’s API’s, SDK’s and developer tools. Developers and startups can utilize these tools to build secure applications faster. 

ThirdFi provides support for blockchain networks including Ethereum, Polygon, BNB Chain, Avalanche, Arbitrum, Optimism, and KCC. Additionally, ThirdFi has already established deep partnerships with top DEXes, such as Uniswap, Sushiswap, Aave, PancakeSwap, TraderJoe, QuickSwap, Velodrome, and more.

Authentication

For authentication, Thirdfi uses two keys

  1. Sandbox/API Key: This key is used for API authentication.
  2. Client Secret Key: It is used to generate the signature/hash and the hash is used in the headers when calling the API.
 

Step by Step guide for building on ThirdFi

1. Create an account on https://app.thirdfi.org/ and you will be redirected to the dashboard like this below.

thirdfi dashboard

 

2. From the setting menu, select sandbox environment for testing purposes. You can do the same with ‘API setup’ instead of selecting ‘Sandbox’ environment.

creating API key sandbox environment

 

3. From the dashboard, click on ‘Create New Sandbox Key’ to get an API Key and the Secret. Save these keys and keep them secret as these will be used to make API calls.

4. Let’s make some API requests now. We already have created a Postman workspace for building and Testing purposes. Head over to our API Docs or click here to get started with Postman Workspace.

5. Our API uses a RESTful interface and uses GET, POST, PATCH and DELETE methods.

6. Below is an example of using the “Get Currency Price” API which uses the “POST” request in Postman.

  • Postman Workspace
postman workspace - calling "get currency price" API
  • Using Code (in Javascript/to be run with NodeJs)
 
let bodyData = JSON.stringify({
“cryptoAmount”: 1,
“cryptoCurrency”: “ETH”,
“fiatCurrency”: “USD”,
“isBuyOrSell”: “BUY”,
“network”: “ethereum”,
“paymentMethod”: “credit_debit_card”
});
 
const URL = `https://sandbox.thirdfi.org/api/v1/payment/currency-price`
const METHOD = ‘POST’
const timestamp = moment().unix()
let baseString = `${URL}&method=${METHOD}&timestamp=${timestamp}
&body=${JSON.stringify(JSON.parse(bodyData))}`
 
const hash = CryptoJS.HmacSHA256(baseString, SECRET_KEY).toString(
CryptoJS.enc.Hex,
)
 
 
Above code contains the constants that are used for requesting data from the API. It is a POST request, so contains a body
 
  • URL – It is the API URL on which the request will be made.
  • TimeStamp – It is the current time calculated in seconds from, check unixtimestamp.com.
  • Body Data – This contains the request body which is used by the API to process the request internally.
  • Base String – It is updated every time as it contains the timestamp and body in it.
 
let config = {
method: METHOD,
maxBodyLength: Infinity,
url: URL,
headers: {
‘x-sec-key’: process.env.API_KEY,
‘x-sec-ts’: timestamp,
‘x-sec-sign’: hash,
‘Content-Type’: ‘application/json’
},
data: bodyData
};
axios.request(config)
.then((response) => {
console.log(JSON.stringify(response.data));
})
.catch((error) => {
console.log(error);
});
 
 
 

API Headers

Three API headers are used in ThirdFi API requests and all of them are required to successfully make an API request. In Postman, we have already added a pre-script to load the API headers but we need to calculate them programmatically. The headers are:

  1. x-sec-key  : ‘API KEY’
  2. x-sec-ts     : ‘Timestamp’
  3. x-sec-sign : ‘Hash Value’

The Hash value is calculated using the HMAC-SHA-256 function which takes two inputs, one is the baseString and the other is the SECRET key.

 

Libraries Used to run the code.

  • Axios         – Promise-based API to implement HTTP requests.
  • cryptojs     – Javascript library for implementing Crypto standards.
  • momentjs  – Javascript date library parsing, validating, manipulating, and formatting dates.

 

Webhooks

ThirdFi also provides webhooks, to get updates based on the API Calls. Webhooks send automated JSON objects based on an event, here we send a response back whenever a transaction is done.

To setup webhooks, you need an endpoint URL to capture the response, you can use this free tool to test our webhook – https://webhook.site/

webhook page

After you have added the webhook, make an API request and make a transaction to see what the response is. You can also see the response in the developer dashboard too, go to Developer Tools -> Webhook events to check it.

Let’s test it, using the “Create Swap Session” API here. Every webhook will have an event type to differentiate between different responses. Here you can find different responses from the webhooks – https://thirdfi.readme.io/reference/webhook-object

webhook event

 

Summary

ThirdFi makes it easy to build DeFi apps. We provide an infrastructure to make Decentralised finance easy and secure. With the Customer API, developers can customize the user experience. Currently, we have the Borrow/Lending API, Swapping API, Fiat-to-crypto API and more, providing decentralised services.

We have only explained the “Payment API” above using NodeJS(Javascript) but most of our API can be called in a similar way. Headers are used in all APIs, which contain the API key, timestamp and hash value. The body is generally used in post, patch and delete methods only.

Feel free to reach out to us on our Socials

Website | Twitter | Discord | Youtube | Linkedin 

Supercharge DeFi with ThirdFi⚡
Code, deploy & profit with ThirdFi

© ThirdFi 2023. All Rights Reserved.

Quick Links

Doc