Setup

Nexus comes with a query-parameter based authentication middleware. It is disabled by default. To enable it, you need to set the rpcAuthKey config in the Nexus configuration. Alternatively, you can set the NEXUS_RPC_AUTH_KEY environment variable.

import {
  Nexus,
  NodeProvider,
  CHAIN,
} from "@whatsgood/nexus";

const llamaRpcNodeProvider = new NodeProvider({
  name: "llama-rpc",
  chain: CHAIN.ETHEREUM_MAINNET,
  url: "https://eth.llamarpc.com",
});


const nexus = Nexus.create({
  // ...
  port: 4000,
  nodeProviders: [llamaRpcNodeProvider],
  rpcAuthKey: 'my-secret-key',
});

Usage

Once you set the rpcAuthKey config, you can start the server and access it with the ?key query parameter.

#                                          ⌄ replace this with your own key
curl -X POST http://localhost:4000/1\?key\=my-secret-key \
     -H "Content-Type: application/json" \
     -d '{
           "jsonrpc": "2.0", 
           "method": "eth_blockNumber", 
           "params": [], 
           "id": 1
         }'