JSON-RPC Methods
Document JSON-RPC methods with parameters, results, and error handling
Methods in OpenRPC are the core building blocks of your JSON-RPC API. Each method defines the procedure name, parameters, return values, and potential errors.
openrpc.yml
methods:- name: user.createsummary: Create a new userdescription: Creates a new user account with the provided informationparams:- name: userDataschema:$ref: '#/components/schemas/CreateUserRequest'required: truedescription: User data for account creationresult:name: createdUserschema:$ref: '#/components/schemas/User'description: The newly created user objectexamples:- name: CreateUserExampledescription: Example of creating a userparams:userData:email: "john@example.com"name: "John Doe"age: 30result:id: "user_123"email: "john@example.com"name: "John Doe"age: 30createdAt: "2024-01-15T10:30:00Z"
Method parameters
Parameters in JSON-RPC can be positional (by-position) or named (by-name):
Named parameters
openrpc.yml
methods:- name: calculate.addsummary: Add two numbersparamStructure: by-nameparams:- name: aschema:type: numberrequired: truedescription: First number- name: bschema:type: numberrequired: truedescription: Second numberresult:name: sumschema:type: number
Positional parameters
openrpc.yml
methods:- name: calculate.multiplysummary: Multiply two numbersparamStructure: by-positionparams:- schema:type: numberdescription: First number (multiplicand)- schema:type: numberdescription: Second number (multiplier)result:name: productschema:type: number
Complex parameter types
Use complex schemas for method parameters:
openrpc.yml
methods:- name: order.createsummary: Create a new orderdescription: Creates a new order with items and shipping informationparams:- name: orderDataschema:type: objectproperties:items:type: arrayitems:$ref: '#/components/schemas/OrderItem'minItems: 1shippingAddress:$ref: '#/components/schemas/Address'paymentMethod:type: stringenum: [credit_card, paypal, bank_transfer]notes:type: stringmaxLength: 500required:- items- shippingAddress- paymentMethodrequired: trueresult:name: orderschema:$ref: '#/components/schemas/Order'
Method results
Define the structure of successful method responses:
openrpc.yml
methods:- name: search.productssummary: Search for productsparams:- name: queryschema:type: stringrequired: trueresult:name: searchResultsschema:type: objectproperties:products:type: arrayitems:$ref: '#/components/schemas/Product'totalCount:type: integerhasMore:type: booleannextCursor:type: stringrequired:- products- totalCount- hasMore
Error handling
Define custom errors for your methods:
openrpc.yml
methods:- name: user.loginsummary: Authenticate userparams:- name: emailschema:type: stringformat: emailrequired: trueerrors:- code: -32001message: Invalid credentialsdata:type: objectproperties:error:type: stringconst: "Email or password is incorrect"- code: -32002message: Account lockeddata:type: objectproperties:error:type: stringconst: "Account temporarily locked due to failed login attempts"unlockTime:type: stringformat: date-timeresult:name: authResultschema:$ref: '#/components/schemas/AuthToken'
Method examples
Provide comprehensive examples for better documentation:
openrpc.yml
methods:- name: file.uploadsummary: Upload a fileparams:- name: fileDataschema:type: objectproperties:filename:type: stringcontent:type: stringformat: base64mimeType:type: stringrequired: trueexamples:- name: ImageUploaddescription: Upload a JPEG imageparams:fileData:filename: "photo.jpg"content: "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAABAAEDASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAv/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFQEBAQAAAAAAAAAAAAAAAAAAAAX/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwC/gA=="mimeType: "image/jpeg"result:fileId: "file_abc123"url: "https://cdn.example.com/files/file_abc123.jpg"size: 1024000- name: DocumentUploaddescription: Upload a PDF documentparams:fileData:filename: "document.pdf"content: "JVBERi0xLjQKJdPr6eEKMSAwIG9iago8PAovVHlwZSAvQ2F0YWxvZwovUGFnZXMgMiAwIFIKPj4KZW5kb2JqCgoyIDAgb2JqCjw8Ci9UeXBlIC9QYWdlcwo="mimeType: "application/pdf"result:fileId: "file_def456"url: "https://cdn.example.com/files/file_def456.pdf"size: 256000
Optional parameters
Define optional parameters with default values:
openrpc.yml
methods:- name: report.generatesummary: Generate a reportparams:- name: reportTypeschema:type: stringenum: [daily, weekly, monthly, yearly]required: true- name: formatschema:type: stringenum: [pdf, csv, json]default: pdfrequired: falsedescription: Output format (defaults to PDF)- name: includeChartsschema:type: booleandefault: truerequired: falsedescription: Include charts in the reportresult:name: reportschema:$ref: '#/components/schemas/Report'
Methods are the foundation of your JSON-RPC API, providing a clear contract for what operations are available, what data they expect, and what they return.