|
| 1 | +--- |
| 2 | +sidebar_position: 3 |
| 3 | +--- |
| 4 | + |
| 5 | +# API |
| 6 | + |
| 7 | +## JSON-RPC API |
| 8 | + |
| 9 | +Both Starknet's and Devnet's JSON-RPC API are reachable at `/rpc` and `/`. E.g. if spawning Devnet with default settings, these URLs are functionally equivalent: `http://127.0.0.1:5050/rpc` and `http://127.0.0.1:5050/`. The difference between these two groups of methods is their prefix: `starknet_` (e.g. `starknet_getNonce`) and `devnet_` (e.g. `devnet_mint`). |
| 10 | + |
| 11 | +### Starknet API |
| 12 | + |
| 13 | +Unlike Pythonic Devnet, which also supported Starknet's gateway and feeder gateway API, Devnet in Rust supports [Starknet's JSON-RPC API](https://github.com/starkware-libs/starknet-specs/tree/master/api), including [WebSocket support](#websocket). |
| 14 | + |
| 15 | +Due to how Devnet internally works, the method `starknet_getStorageProof` is not applicable, and thus not supported. |
| 16 | + |
| 17 | +Since JSON-RPC v0.6.0, to find out which JSON-RPC version is supported by which Devnet version, check out the [releases page](https://github.com/0xspaceshard/starknet-devnet/releases). |
| 18 | + |
| 19 | +### Devnet API |
| 20 | + |
| 21 | +Devnet has many additional features available via JSON-RPC. The RPC methods are documented throughout the documentation in their corresponding pages, but are also aggregated [here](https://github.com/0xSpaceShard/starknet-devnet/blob/main/website/static/devnet_api.json). |
| 22 | + |
| 23 | +#### Healthcheck |
| 24 | + |
| 25 | +To check if a Devnet instance is alive, send an HTTP request `GET /is_alive`. If alive, Devnet will reply with a `200 OK` and an appropriate message. This is the only special functionality not provided as part of the JSON-RPC API. |
| 26 | + |
| 27 | +### WebSocket |
| 28 | + |
| 29 | +The whole [Starknet](#starknet-api) and [Devnet](#devnet-api) JSON-RPC API, including [WebSocket subscription methods](https://github.com/starkware-libs/starknet-specs/blob/v0.9.0/api/starknet_ws_api.json) can be accessed via the WebSocket protocol, using text or binary messages. Devnet listens for new WebSocket connections at `ws://<HOST>:<PORT>/ws` (notice the protocol scheme). E.g. using [`wscat`](https://www.npmjs.com/package/wscat) on the same computer where Devnet is spawned at default host and port: |
| 30 | + |
| 31 | +``` |
| 32 | +$ wscat -c ws://127.0.0.1:5050/ws |
| 33 | +Connected (press CTRL+C to quit) |
| 34 | +> { "jsonrpc": "2.0", "id": 0, "method": "starknet_subscribeNewHeads" } |
| 35 | +< {"id":0,"result":2935616350010920547,"jsonrpc":"2.0"} |
| 36 | +``` |
| 37 | + |
| 38 | +#### WebSocket persistence |
| 39 | + |
| 40 | +[Restarting](./dump-load-restart#restarting) and [loading](./dump-load-restart#loading) do not affect Devnet's WebSocket connections, but remove all subscriptions. |
| 41 | + |
| 42 | +## Interacting with Devnet in JavaScript and TypeScript |
| 43 | + |
| 44 | +To spawn Devnet and interact with it using the [Devnet API](#devnet-api), you can use [`starknet-devnet-js`](https://github.com/0xSpaceShard/starknet-devnet-js/). This can be especially useful in achieving [L1-L2 communication](./postman.md). |
| 45 | + |
| 46 | +To interact with Devnet using the [Starknet API](#starknet-api), use [starknet.js](https://www.starknetjs.com/). |
| 47 | + |
| 48 | +## Config API |
| 49 | + |
| 50 | +To retrieve the current configuration of Devnet, as specified via [CLI](running/cli.md) and later requests, send a `JSON-RPC` request with method name `devnet_getConfig`. Example response is attached below. It can be interpreted as a JSON mapping of CLI input parameters, both specified and default ones, with some irrelevant parameters omitted. So use `starknet-devnet --help` to better understand the meaning of each value, though keep in mind that some of the parameters have slightly modified names. The exact values may have changed and should not be referenced. |
| 51 | + |
| 52 | +```json |
| 53 | +{ |
| 54 | + "seed": 4063802897, |
| 55 | + "total_accounts": 10, |
| 56 | + "account_contract_class_hash": "0x61dac032f228abef9c6626f995015233097ae253a7f72d68552db02f2971b8f", |
| 57 | + "predeployed_accounts_initial_balance": "1000000000000000000000", |
| 58 | + "start_time": null, |
| 59 | + "gas_price_wei": 100000000000, |
| 60 | + "gas_price_fri": 100000000000, |
| 61 | + "data_gas_price_wei": 100000000000, |
| 62 | + "data_gas_price_fri": 100000000000, |
| 63 | + "l2_gas_price_wei": 100000000000, |
| 64 | + "l2_gas_price_fri": 100000000000, |
| 65 | + "chain_id": "SN_SEPOLIA", |
| 66 | + "dump_on": "exit", |
| 67 | + "dump_path": "dump_path.json", |
| 68 | + "state_archive": "none", |
| 69 | + "fork_config": { |
| 70 | + "url": "http://rpc.pathfinder.equilibrium.co/integration-sepolia/rpc/v0_7", |
| 71 | + "block_number": 26429 |
| 72 | + }, |
| 73 | + "server_config": { |
| 74 | + "host": "127.0.0.1", |
| 75 | + "port": 5050, |
| 76 | + "timeout": 120, |
| 77 | + "restricted_methods": null |
| 78 | + }, |
| 79 | + "block_generation": null, |
| 80 | + "lite_mode": false, |
| 81 | + "eth_erc20_class_hash": "0x046ded64ae2dead6448e247234bab192a9c483644395b66f2155f2614e5804b0", |
| 82 | + "strk_erc20_class_hash": "0x046ded64ae2dead6448e247234bab192a9c483644395b66f2155f2614e5804b0" |
| 83 | +} |
| 84 | +``` |
0 commit comments