Use the Thallo SDK NPM library to integrate at lightning speed âš¡
Example Code
The full working code for this example can be found here.
In this example we do the following:
Get the market data, this contains sell orders for specific projects and vintages, including quantity available and the price per tonne.
Get the rich project data for all projects that have credits for sale.
Find a project that has been implemented in Sierra Leone.
Ensure the price is within your defined boundaries.
Purchase 1 whole tonne and retire 20 grams of the credits in one atomic action.
The purchase will only be made if you do not have 20 grams for this project and vintage available in your CaaS inventory
// get market dataconstmarketData= (awaithttpClient.get('/api/market')).data// get project ids so we can get the project detailsconstprojectIds=marketData.projects.map((project: { project_id:string }) =>project.project_id)// we should cache the project data as it doesn't change oftenconstprojects= (awaithttpClient.get('/api/projects', { params: { project_ids: projectIds }})).data.projects// select project from Sierra LeoneconstselectedProject=projects.filter((project: { location:string }) =>project.location ==='SL')[0]// get sell order for this projectconstvintage=marketData.projects.filter((project: { project_id:string }) =>project.project_id ===selectedProject.project_id)[0] .vintages[0]constsellOrder=vintage.sell_orders[0]// ensure the price is less than the max we're comfortable paying e.g. $10.45if (BigNumber.from(sellOrder.price_cents).gt(1045)) {thrownewError('Sell order price too high')}// request retirementconstretirementRequestId= (awaithttpClient.post('api/caas/request-retirement', { quantity_grams:'20', vintage_id:vintage.vintage_id, sell_order_id:sellOrder.sell_order_id, expected_price_cents:sellOrder.price_cents,})).data.retirement_request_id// get retirement detailsconstretirement= (awaithttpClient.post('api/retirement', { params: { id: retirementRequestId }})).data