2019-12-01 08:21:20 +00:00
|
|
|
# API
|
|
|
|
|
2019-12-09 12:53:40 +00:00
|
|
|
Here is an early overview of how to make API calls.
|
2019-12-04 15:39:41 +00:00
|
|
|
|
2019-12-18 18:40:47 +00:00
|
|
|
Implementation details for the **JSON-RPC** package are
|
|
|
|
[here](https://github.com/filecoin-project/lotus/tree/master/lib/jsonrpc).
|
2019-12-01 08:21:20 +00:00
|
|
|
|
2019-12-11 15:38:04 +00:00
|
|
|
## Overview: How do you modify the config.toml to change the API endpoint?
|
2019-12-01 08:21:20 +00:00
|
|
|
|
2019-12-18 18:40:47 +00:00
|
|
|
API requests are made against `127.0.0.1:1234` unless you modify
|
|
|
|
`.lotus/config.toml`.
|
2019-12-01 08:21:20 +00:00
|
|
|
|
2019-12-09 12:53:40 +00:00
|
|
|
Options:
|
2019-12-01 08:21:20 +00:00
|
|
|
|
2019-12-09 12:53:40 +00:00
|
|
|
- `http://[api:port]/rpc/v0` - HTTP endpoint
|
2019-12-18 18:40:47 +00:00
|
|
|
- `ws://[api:port]/rpc/v0` - Websocket endpoint
|
|
|
|
- `PUT http://[api:port]/rest/v0/import` - File import, it requires write
|
|
|
|
permissions.
|
2019-12-01 08:21:20 +00:00
|
|
|
|
2019-12-09 12:53:40 +00:00
|
|
|
## What methods can I use?
|
2019-12-01 08:21:20 +00:00
|
|
|
|
2019-12-18 18:40:47 +00:00
|
|
|
For now, you can look into different files to find methods available to you
|
|
|
|
based on your needs:
|
2019-12-11 01:50:13 +00:00
|
|
|
|
2019-12-18 18:40:47 +00:00
|
|
|
- [Both Lotus node + storage miner APIs](https://github.com/filecoin-project/lotus/blob/master/api/api_common.go)
|
|
|
|
- [Lotus node API](https://github.com/filecoin-project/lotus/blob/master/api/api_full.go)
|
|
|
|
- [Storage miner API](https://github.com/filecoin-project/lotus/blob/master/api/api_storage.go)
|
2019-12-01 08:21:20 +00:00
|
|
|
|
2019-12-18 18:40:47 +00:00
|
|
|
The necessary permissions for each are in
|
|
|
|
[api/struct.go](https://github.com/filecoin-project/lotus/blob/master/api/struct.go).
|
2019-12-01 08:21:20 +00:00
|
|
|
|
2019-12-09 12:53:40 +00:00
|
|
|
## How do I make an API request?
|
|
|
|
|
2019-12-18 18:40:47 +00:00
|
|
|
To demonstrate making an API request, we will take the method `ChainHead` from
|
|
|
|
[api/api.go](https://github.com/filecoin-project/lotus/blob/master/api/api_full.go).
|
2019-12-09 12:53:40 +00:00
|
|
|
|
|
|
|
```go
|
|
|
|
ChainHead(context.Context) (*types.TipSet, error)
|
|
|
|
```
|
|
|
|
|
2019-12-18 18:40:47 +00:00
|
|
|
And create a CURL command. In this command, `ChainHead` is included as
|
|
|
|
`{ "method": "Filecoin.ChainHead" }`:
|
2019-12-09 12:53:40 +00:00
|
|
|
|
|
|
|
```sh
|
|
|
|
curl -X POST \
|
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
--data '{ "jsonrpc": "2.0", "method": "Filecoin.ChainHead", "params": [], "id": 3 }' \
|
|
|
|
'http://127.0.0.1:1234/rpc/v0'
|
|
|
|
```
|
|
|
|
|
|
|
|
If the request requires authorization, add an authorization header:
|
|
|
|
|
|
|
|
```sh
|
|
|
|
curl -X POST \
|
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
-H "Authorization: Bearer $(cat ~/.lotusstorage/token)" \
|
|
|
|
--data '{ "jsonrpc": "2.0", "method": "Filecoin.ChainHead", "params": [], "id": 3 }' \
|
|
|
|
'http://127.0.0.1:1234/rpc/v0'
|
|
|
|
```
|
|
|
|
|
2019-12-18 18:40:47 +00:00
|
|
|
> In the future we will add a playground to make it easier to build and
|
|
|
|
> experiment with API requests.
|
2019-12-09 12:53:40 +00:00
|
|
|
|
2019-12-10 10:01:26 +00:00
|
|
|
## CURL authorization
|
2019-12-09 12:53:40 +00:00
|
|
|
|
2019-12-18 18:40:47 +00:00
|
|
|
To authorize your request, you will need to include the **JWT** in a HTTP
|
|
|
|
header, for example:
|
2019-12-09 12:53:40 +00:00
|
|
|
|
|
|
|
```sh
|
|
|
|
-H "Authorization: Bearer $(cat ~/.lotusstorage/token)"
|
|
|
|
```
|
|
|
|
|
2019-12-18 18:40:47 +00:00
|
|
|
Admin token is stored in `~/.lotus/token` for the **Lotus Node** or
|
|
|
|
`~/.lotusstorage/token` for the **Lotus Storage Miner**.
|
2019-12-09 12:53:40 +00:00
|
|
|
|
2019-12-10 10:01:26 +00:00
|
|
|
## How do I generate a token?
|
|
|
|
|
|
|
|
To generate a JWT with custom permissions, use this command:
|
|
|
|
|
|
|
|
```sh
|
|
|
|
# Lotus Node
|
|
|
|
lotus auth create-token --perm admin
|
|
|
|
|
|
|
|
# Lotus Storage Miner
|
|
|
|
lotus-storage-miner auth create-token --perm admin
|
|
|
|
```
|
|
|
|
|
|
|
|
## What authorization level should I use?
|
2019-12-09 12:53:40 +00:00
|
|
|
|
2019-12-18 18:40:47 +00:00
|
|
|
When viewing
|
|
|
|
[api/struct.go](https://github.com/filecoin-project/lotus/blob/master/api/struct.go),
|
|
|
|
you will encounter these types:
|
2019-12-09 12:53:40 +00:00
|
|
|
|
|
|
|
- `read` - Read node state, no private data.
|
2019-12-10 10:01:26 +00:00
|
|
|
- `write` - Write to local store / chain, and `read` permissions.
|
2019-12-18 18:40:47 +00:00
|
|
|
- `sign` - Use private keys stored in wallet for signing, `read` and `write`
|
|
|
|
permissions.
|
2019-12-10 10:01:26 +00:00
|
|
|
- `admin` - Manage permissions, `read`, `write`, and `sign` permissions.
|