JSON-RPC and JavaScript

Calling the JSON-RPC using JavaScript from a web application is very simple, as HTTP and Websockets are part of almost every web browser, and JSON parsing is built into JavaScript. For example, the following code uses the Fetch API and async/await features found in any modern browser to query the Filecoin.Version JSON-RPC method via HTTP:

async getVersion () {
  const response = await fetch('', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    body: JSON.stringify({
      jsonrpc: '2.0',
      method: `Filecoin.Version`,
      params: [],
      id: 1
  const { result, error } = await response.json()

Caution: There is a big problem with the above code. Due to the web's Same-origin policy, the above code will not usually work unmodified in a web page as the API endpoint is likely on a different "origin" than the location the web page is hosted at. Read more about how to set up an endpoint in the endpoint deployment chapter.