Skip to content

Latest commit

 

History

History
61 lines (46 loc) · 2.12 KB

03-guides-example.md

File metadata and controls

61 lines (46 loc) · 2.12 KB
id title hide_title
guides-example
Guides example
true

Guides example

Before getting into coding, you are going to have to start a Lotus node. To get started quickly, you can use a hosted node by Glif. In this example, we can use a RPC endpoint for calibration net:

https://api.calibration.node.glif.io/rpc/v0

Node.js and front-end frameworks

You can use ES6 style and/or use it in your Typescript.

import { HttpJsonRpcConnector, LotusClient } from 'filecoin.js';

(async() => {
  const httpConnector = new HttpJsonRpcConnector('https://api.calibration.node.glif.io/rpc/v0');
  const lotusClient = new LotusClient(httpConnector);
  const version = await lotusClient.common.version();
  console.log(`Hello, Filecoin.js v${version}`);
})();

Node.js polyfill

⚠️ Because many Node.js dependencies are not compatible on the front end, if you faced an issue like this, chances are you will have to add a polyfills as a dependency.

Browser

Just drop Filecoin.js into the <script> tag and you're ready to go.

<script type="text/javascript" src="https://unpkg.com/filecoin.js" />
<script type="text/javascript">
  (async () => {
    const httpConnector = new FilecoinJs.HttpJsonRpcConnector('https://api.calibration.node.glif.io/rpc/v0');

    const lotusClient = new FilecoinJs.LotusClient(httpConnector);
    const version = await lotusClient.common.version();
    console.log(version);
  })();
</script>

If you run your own node, you will have to provide an argument of type JsonRpcConnectionOptions to HttpJsonRpcConnector constructor.

const httpConnector = new HttpJsonRpcConnector({
  url: __LOTUS_HTTP_RPC_ENDPOINT__,
  token: __LOTUS_AUTH_TOKEN__,
});