id | title | hide_title |
---|---|---|
guides-example |
Guides example |
true |
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
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}`);
})();
⚠️ 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.
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__,
});