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
1methods:
2 - name: user.create
3 summary: Create a new user
4 description: Creates a new user account with the provided information
5 params:
6 - name: userData
7 schema:
8 $ref: '#/components/schemas/CreateUserRequest'
9 required: true
10 description: User data for account creation
11 result:
12 name: createdUser
13 schema:
14 $ref: '#/components/schemas/User'
15 description: The newly created user object
16 examples:
17 - name: CreateUserExample
18 description: Example of creating a user
19 params:
20 userData:
21 email: "john@example.com"
22 name: "John Doe"
23 age: 30
24 result:
25 id: "user_123"
26 email: "john@example.com"
27 name: "John Doe"
28 age: 30
29 createdAt: "2024-01-15T10:30:00Z"

Method parameters

Parameters in JSON-RPC can be positional (by-position) or named (by-name):

Named parameters

openrpc.yml
1methods:
2 - name: calculate.add
3 summary: Add two numbers
4 paramStructure: by-name
5 params:
6 - name: a
7 schema:
8 type: number
9 required: true
10 description: First number
11 - name: b
12 schema:
13 type: number
14 required: true
15 description: Second number
16 result:
17 name: sum
18 schema:
19 type: number

Positional parameters

openrpc.yml
1methods:
2 - name: calculate.multiply
3 summary: Multiply two numbers
4 paramStructure: by-position
5 params:
6 - schema:
7 type: number
8 description: First number (multiplicand)
9 - schema:
10 type: number
11 description: Second number (multiplier)
12 result:
13 name: product
14 schema:
15 type: number

Complex parameter types

Use complex schemas for method parameters:

openrpc.yml
1methods:
2 - name: order.create
3 summary: Create a new order
4 description: Creates a new order with items and shipping information
5 params:
6 - name: orderData
7 schema:
8 type: object
9 properties:
10 items:
11 type: array
12 items:
13 $ref: '#/components/schemas/OrderItem'
14 minItems: 1
15 shippingAddress:
16 $ref: '#/components/schemas/Address'
17 paymentMethod:
18 type: string
19 enum: [credit_card, paypal, bank_transfer]
20 notes:
21 type: string
22 maxLength: 500
23 required:
24 - items
25 - shippingAddress
26 - paymentMethod
27 required: true
28 result:
29 name: order
30 schema:
31 $ref: '#/components/schemas/Order'

Method results

Define the structure of successful method responses:

openrpc.yml
1methods:
2 - name: search.products
3 summary: Search for products
4 params:
5 - name: query
6 schema:
7 type: string
8 required: true
9 result:
10 name: searchResults
11 schema:
12 type: object
13 properties:
14 products:
15 type: array
16 items:
17 $ref: '#/components/schemas/Product'
18 totalCount:
19 type: integer
20 hasMore:
21 type: boolean
22 nextCursor:
23 type: string
24 required:
25 - products
26 - totalCount
27 - hasMore

Error handling

Define custom errors for your methods:

openrpc.yml
1methods:
2 - name: user.login
3 summary: Authenticate user
4 params:
5 - name: email
6 schema:
7 type: string
8 format: email
9 required: true
10 errors:
11 - code: -32001
12 message: Invalid credentials
13 data:
14 type: object
15 properties:
16 error:
17 type: string
18 const: "Email or password is incorrect"
19 - code: -32002
20 message: Account locked
21 data:
22 type: object
23 properties:
24 error:
25 type: string
26 const: "Account temporarily locked due to failed login attempts"
27 unlockTime:
28 type: string
29 format: date-time
30 result:
31 name: authResult
32 schema:
33 $ref: '#/components/schemas/AuthToken'

Method examples

Provide comprehensive examples for better documentation:

openrpc.yml
1methods:
2 - name: file.upload
3 summary: Upload a file
4 params:
5 - name: fileData
6 schema:
7 type: object
8 properties:
9 filename:
10 type: string
11 content:
12 type: string
13 format: base64
14 mimeType:
15 type: string
16 required: true
17 examples:
18 - name: ImageUpload
19 description: Upload a JPEG image
20 params:
21 fileData:
22 filename: "photo.jpg"
23 content: "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAABAAEDASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAv/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFQEBAQAAAAAAAAAAAAAAAAAAAAX/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwC/gA=="
24 mimeType: "image/jpeg"
25 result:
26 fileId: "file_abc123"
27 url: "https://cdn.example.com/files/file_abc123.jpg"
28 size: 1024000
29 - name: DocumentUpload
30 description: Upload a PDF document
31 params:
32 fileData:
33 filename: "document.pdf"
34 content: "JVBERi0xLjQKJdPr6eEKMSAwIG9iago8PAovVHlwZSAvQ2F0YWxvZwovUGFnZXMgMiAwIFIKPj4KZW5kb2JqCgoyIDAgb2JqCjw8Ci9UeXBlIC9QYWdlcwo="
35 mimeType: "application/pdf"
36 result:
37 fileId: "file_def456"
38 url: "https://cdn.example.com/files/file_def456.pdf"
39 size: 256000

Optional parameters

Define optional parameters with default values:

openrpc.yml
1methods:
2 - name: report.generate
3 summary: Generate a report
4 params:
5 - name: reportType
6 schema:
7 type: string
8 enum: [daily, weekly, monthly, yearly]
9 required: true
10 - name: format
11 schema:
12 type: string
13 enum: [pdf, csv, json]
14 default: pdf
15 required: false
16 description: Output format (defaults to PDF)
17 - name: includeCharts
18 schema:
19 type: boolean
20 default: true
21 required: false
22 description: Include charts in the report
23 result:
24 name: report
25 schema:
26 $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.