⌨️API Functionality
Co2.Storage has a Javascript API, which enables users to interact with data schemas and assets in a number of ways. The API is available on NPM here: CO2.Storage API. Below are descriptions of the various API endpoints:
Authenticate: this endpoint allows users to authenticate with a private key using the
Authclass.This endpoint exports a single function called
authenticate()which returns aPromisethat resolves to either anerroror aresultproperty.To use the endpoint, users should import the
Authclass, and then create a new instance of it with the authentication type set to"pk". Once an instance ofAuthis created, users can call theauthenticate()function to authenticate with their private key.If the authentication is successful, the
authenticate()function returns an object with aresultproperty that contains the authenticated user's information. If the authentication fails, theauthenticate()function returns anerrorthat explains the reason for the failure.
Add Template: this endpoint allows the user to add a template to CO2.Storage.
The template is defined as a JSON object and must include a
typeandmandatoryfields for each template attribute.The
templateNameis a string that gives the template a name.The
templateBaseis an object that includes atitleandreferencefields that define the base of the template.The
templateDescriptionis a string that provides a description of the template.The
templateParentis the CID of the template that the new template should inherit from.The
chainNameis a string that defines the branch of the IPLD DAG from which the template was created.
The endpoint instantiates a new object with the provided authentication, IPFS node type, IPFS node address, and API URL. The endpoint then calls the
addTemplate()method of the FGStorage object to add the template to IPFS. The endpoint will return a response containing the added templates CID.A full list of the supported data types can be found here: CO2.Storage Supported Data Types
Add Asset: this endpoint allows the user to add an asset to CO2.Storage.
To use the endpoint, users must import
@co2-storage/js-apiandfs.The endpoint requires the following parameters:
assetElements: a JSON object containing asset dataparent: a string containing the CID of the asset's parentname: a string containing the asset's namedescription: a string containing the asset's descriptiontemplate: a string containing the CID of the asset's templatefilesUploadStart: a function that is called when the file upload startsfilesUploadProgress: a function to track file upload progressfilesUploadEnd: a function that will be called when the file upload endsassetCreationStart: a function that will be called when asset creation startsassetCreationEnd: a function that will be called when asset creation ends
The endpoint will return a response containing the added asset's CID.
Get Template: this endpoint allows users to to search for and retrieve templates from CO2.Storage.
This endpoint takes ten optional parameters:
chainName,phrases,cid,name,base,account,offset,limit,sortBy, andsortDir.By default, the
chainNameparameter is set to'sandbox'and the other parameters are set tonullor0.The function returns a response object that includes the search results in
result.templatesor an error inerror.The
getTemplatefunction is used to retrieve a specific template from Co2.Storage. It takes a single parameter, theblockCID of the desired template.The function returns a response object that includes the template data in
resultor an error inerror.The script uses
searchTemplatesto find the most recently listed template and then usesgetTemplateto retrieve that template's data.
Get Asset: this endpoint endpoint allows users to search for and retrieve assets stored on CO2.Storage.
The API searches for assets using the
searchAssetsmethod, which takes several optional parameters such asphrases,cid,name,base,account,offset,limit,sortBy, andsortDir.The API retrieves the last listed asset using the block CID returned by the search. The
getAssetmethod is used to retrieve the asset, passing in the block CID as the parameter. The retrieved asset is printed to the console usingconsole.dir, and the program waits for 1 second before exiting.
Search Templates: this endpoint allows users to search for templates on CO2.Storage.
The endpoint takes in several parameters, including
chainName,phrases,cid,name,base,account,offset,limit,sortBy, andsortDir, which can be used to filter and sort the search results.The default values for these parameters are
sandbox,null,null,null,null,null,0,10,null, andnull, respectively.
The response returns a JSON object containing information on the searched templates, including the template's name, version, block CID, and metadata.
Search Assets: this endpoint allows users to search for assets on CO2.Storage.
The endpoint takes in parameters including
chainName,phrases,cid,name,base,account,offset,limit,sortBy, andsortDir, with default values set for several of these parameters.The endpoint returns a JSON response containing a
resultobject with information about the assets matching the search parameters, including their names, block IDs, and other metadata.
Get Account: this endpoint retrieves the details of one account through the
getAccountfunction.The endpoint takes one parameter,
chainName, which is a string indicating the name of the environment where the account is located.The endpoint returns an object with information about the account, including its address, balance, and nonce.
Get Accounts: this endpoint retrieves the details of all accounts in a given environment through the
getAccountsfunction.The endpoint requires a
chainNameparameter, which specifies the environment to search for accounts on.The function returns an object with two properties:
errorandresult.The
errorproperty is null if there were no errors, and contains an error message if an error was encountered.The
resultproperty contains an array of objects representing the accounts associated with the specified chain.
Sign CID's: this endpoint is for signing a CID of an Asset on CO2.Storage
It signs the CID with the private key provided in the authentication step.
The endpoint takes two parameters:
blockCidas a string representing the CID of the block to be signed, andcallbackas a function that takes aresponseobject as its parameter.The response object contains the result of the sign CID operation, including the signature and related information. After signing the CID, the function outputs the response object.
Last updated