JSON-RPC Code Generator Tool

Generate client and server code for JSON-RPC APIs in multiple programming languages with a few clicks.

On this page

Ready to generate code?

Try the Code Generator tool now to create client and server code.

Go to Code Generator

Overview

The JSON-RPC Code Generator tool simplifies the process of implementing JSON-RPC clients and servers by automatically generating boilerplate code in multiple programming languages. This tool is particularly useful for developers who want to ensure their code follows best practices and adheres to the JSON-RPC specification.

Pro Tip: Use the code generator to create consistent client-server implementations across different programming languages and frameworks.

Features

Multi-language Support

Generate code in JavaScript, Python, Java, Go, and more

Client & Server Code

Create both client and server implementations

Framework Integration

Support for popular frameworks like Express, Flask, and Spring

Customizable Templates

Adjust code generation to match your project requirements

Type-safe Code

Generate strongly-typed code for supported languages

Export Options

Save generated code as files or copy to clipboard

Supported Languages

The Code Generator supports a wide range of programming languages commonly used for web and API development.

JavaScript/TypeScript

Node.js, browser clients

Python

Python 3.x support

Java

Java 8+ compatible

Go

Modern Go with modules

PHP

PHP 7.4+ compatible

Ruby

Ruby 2.6+ support

C#

.NET Core and .NET Framework

Rust

Modern Rust with Cargo

Dart

Flutter compatible

Usage Guide

Basic Usage

To generate code with the Code Generator tool:

  1. Define your JSON-RPC methods and parameters in the interface definition
  2. Select your target programming language
  3. Choose whether to generate client code, server code, or both
  4. Configure any language-specific options
  5. Click "Generate Code" to create the implementation
  6. Copy or download the generated code

Interface Definition Example

{
  "methods": [
    {
      "name": "getUserById",
      "params": {
        "id": {
          "type": "number",
          "description": "User ID"
        }
      },
      "returns": {
        "type": "object",
        "properties": {
          "id": {"type": "number"},
          "name": {"type": "string"},
          "email": {"type": "string"}
        }
      }
    },
    {
      "name": "createUser",
      "params": {
        "user": {
          "type": "object",
          "properties": {
            "name": {"type": "string"},
            "email": {"type": "string"}
          }
        }
      },
      "returns": {
        "type": "object",
        "properties": {
          "id": {"type": "number"},
          "name": {"type": "string"},
          "email": {"type": "string"}
        }
      }
    }
  ]
}

Customization

The Code Generator tool offers several customization options to tailor the generated code to your project requirements.

Customization Options

Naming Conventions

Adjust naming conventions for classes, methods, and variables to match your project style guide.

Package/Namespace

Specify custom package or namespace names for the generated code.

Error Handling

Configure how errors are handled in the generated code (exceptions, error objects, etc.)

Documentation

Include method and parameter descriptions in the generated code as comments.

Framework Support

The Code Generator can integrate with popular frameworks to create more idiomatic JSON-RPC implementations.

JavaScript/TypeScript
  • Express.js
  • Koa
  • Fastify
  • Axios (client)
Python
  • Flask
  • Django
  • FastAPI
  • Requests (client)
Java
  • Spring Boot
  • Quarkus
  • Jersey
  • OkHttp (client)
Go
  • Gin
  • Echo
  • Fiber
  • Standard net/http

Examples

JavaScript Client Example

// Generated JSON-RPC client for the User API
class UserApiClient {
  constructor(endpoint) {
    this.endpoint = endpoint;
    this.requestId = 1;
  }

  async getUserById(id) {
    const response = await this._sendRequest('getUserById', { id });
    return response.result;
  }

  async createUser(user) {
    const response = await this._sendRequest('createUser', { user });
    return response.result;
  }

  async _sendRequest(method, params) {
    const response = await fetch(this.endpoint, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        jsonrpc: '2.0',
        method,
        params,
        id: this.requestId++
      })
    });
    
    const data = await response.json();
    
    if (data.error) {
      throw new Error(`${data.error.code}: ${data.error.message}`);
    }
    
    return data;
  }
}

Python Server Example

# Generated JSON-RPC server handlers for Flask
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/api/jsonrpc', methods=['POST'])
def handle_jsonrpc():
    request_data = request.get_json()
    
    if not isinstance(request_data, dict):
        return jsonify({
            "jsonrpc": "2.0",
            "error": {"code": -32600, "message": "Invalid Request"},
            "id": None
        }), 400
    
    method = request_data.get('method')
    params = request_data.get('params', {})
    req_id = request_data.get('id')
    
    # Method dispatch
    handlers = {
        'getUserById': handle_get_user_by_id,
        'createUser': handle_create_user
    }
    
    if method not in handlers:
        return jsonify({
            "jsonrpc": "2.0",
            "error": {"code": -32601, "message": "Method not found"},
            "id": req_id
        }), 404
    
    try:
        result = handlers[method](params)
        return jsonify({
            "jsonrpc": "2.0",
            "result": result,
            "id": req_id
        })
    except Exception as e:
        return jsonify({
            "jsonrpc": "2.0",
            "error": {"code": -32603, "message": str(e)},
            "id": req_id
        }), 500

def handle_get_user_by_id(params):
    user_id = params.get('id')
    # Implementation would go here
    return {"id": user_id, "name": "Example User", "email": "[email protected]"}

def handle_create_user(params):
    user = params.get('user', {})
    # Implementation would go here
    return {"id": 123, "name": user.get('name'), "email": user.get('email')}

if __name__ == '__main__':
    app.run(debug=True)