Skip to content

Basic Usage

Vsevolod Trofimov edited this page Jul 6, 2018 · 3 revisions

This guide has examples for node js. If you are planning to use casperapi directly form the browser, please read the readme of this example afterwards. Even though api is almost identical there are some browser specific types and behaviour.

Import web3 and Casper libriraies

const Web3 = require('web3') // or web3-eth
const CasperApi = require('casperapi')

Create web3 instance and connect it to casper test network via http

const web3 = new Web3('http://94.130.182.144:8775')

CasperApi needs to be passed blockchain provider directly, as more blockchains would be available soon.

const casper = new CasperApi(web3)
const savePromise = casper.save(Buffer.from('My buffer'))

The value of savePromise is an extended native Promise.

casper.save makes requests to smart contract and provider nodes, uploading file on the with highest speed & stablity. savePromise would only resolve after the file has been fully uploaded.

savePromise
  .then(uuid => casper.getFile(uuid))
  .then(file => console.log(file.toString()))

Let's break down this one:

savePromise resolves with uuid: file's unique identifier in casper network. You'll use it to fetch, modifiy and delete the file.

Then we use casper.getFile that fetches the file as Buffer and returns it.

Clone this wiki locally