Skip to content

Value Objects

All OpenRPC specification components are represented as immutable value objects extending Spatie\LaravelData\Data. This ensures type safety, automatic serialization, and integration with Laravel’s ecosystem.

The root container for an OpenRPC specification document.

use Cline\OpenRpc\ValueObject\DocumentValue;
$document = new DocumentValue(
openrpc: '1.2.6', // Required: OpenRPC spec version
info: $info, // Required: API metadata
servers: $servers, // Optional: Server endpoints
methods: $methods, // Required: RPC methods
components: $components, // Optional: Reusable components
externalDocs: $externalDocs, // Optional: External documentation
);

Properties:

PropertyTypeRequiredDescription
openrpcstringYesSemantic version of OpenRPC specification
infoInfoValueYesAPI metadata
serversDataCollection<ServerValue>NoAvailable server endpoints
methodsDataCollection<MethodValue>YesRPC method definitions
componentsComponentsValueNoReusable specification components
externalDocsExternalDocumentationValueNoExternal documentation links

Metadata about the API including title, version, and contact information.

use Cline\OpenRpc\ValueObject\InfoValue;
use Cline\OpenRpc\ValueObject\ContactValue;
use Cline\OpenRpc\ValueObject\LicenseValue;
$info = new InfoValue(
title: 'My API',
description: 'A JSON-RPC 2.0 API',
termsOfService: 'https://example.com/tos',
contact: new ContactValue(
name: 'API Support',
url: 'https://example.com/support',
email: 'support@example.com',
),
license: new LicenseValue(
name: 'MIT',
url: 'https://opensource.org/licenses/MIT',
),
version: '1.0.0',
);

Properties:

PropertyTypeRequiredDescription
titlestringYesAPI title
description?stringNoDetailed description (CommonMark)
termsOfService?stringNoURL to terms of service
contact?ContactValueNoContact information
license?LicenseValueNoLicense information
versionstringYesAPI version

Defines an RPC method with its parameters, result, and metadata.

use Cline\OpenRpc\ValueObject\MethodValue;
use Cline\OpenRpc\ValueObject\ContentDescriptorValue;
use Spatie\LaravelData\DataCollection;
$method = new MethodValue(
name: 'user.create',
tags: null,
summary: 'Create a new user',
description: 'Creates a user with the provided data',
externalDocs: null,
params: new DataCollection(ContentDescriptorValue::class, [
new ContentDescriptorValue(
name: 'email',
summary: 'User email address',
description: null,
required: true,
schema: ['type' => 'string', 'format' => 'email'],
deprecated: false,
),
new ContentDescriptorValue(
name: 'name',
summary: 'User display name',
description: null,
required: true,
schema: ['type' => 'string', 'minLength' => 1],
deprecated: false,
),
]),
result: new ContentDescriptorValue(
name: 'user',
summary: 'Created user object',
description: null,
required: true,
schema: ['$ref' => '#/components/schemas/User'],
deprecated: false,
),
deprecated: false,
servers: null,
errors: null,
links: null,
paramStructure: 'by-name',
examples: null,
);

Properties:

PropertyTypeRequiredDescription
namestringYesUnique method identifier
tagsDataCollection<TagValue>NoCategorization tags
summary?stringNoBrief description
description?stringNoDetailed description
externalDocs?ExternalDocumentationValueNoExternal documentation
paramsDataCollection<ContentDescriptorValue>YesMethod parameters
result?ContentDescriptorValueNoReturn value descriptor
deprecatedboolNoDeprecation flag
serversDataCollection<ServerValue>NoMethod-specific servers
errorsDataCollection<ErrorValue>NoPossible error responses
linksDataCollection<LinkValue>NoRelated resources
paramStructure?stringNoby-position or by-name
examplesDataCollection<ExamplePairingValue>NoExample request/response pairs

Describes a parameter, result, or error with its schema and metadata.

use Cline\OpenRpc\ValueObject\ContentDescriptorValue;
$descriptor = new ContentDescriptorValue(
name: 'userId',
summary: 'User identifier',
description: 'The unique identifier for a user in the system',
required: true,
schema: [
'type' => 'integer',
'minimum' => 1,
'description' => 'Positive integer ID',
],
deprecated: false,
);

Properties:

PropertyTypeRequiredDescription
namestringYesUnique identifier
summary?stringNoBrief description
description?stringNoDetailed description
requiredboolNoWhether parameter is required
schema?arrayNoJSON Schema definition
deprecatedboolNoDeprecation flag

Defines an API server endpoint.

use Cline\OpenRpc\ValueObject\ServerValue;
use Cline\OpenRpc\ValueObject\ServerVariableValue;
$server = new ServerValue(
name: 'Production',
url: 'https://api.example.com/{version}/rpc',
summary: 'Production API endpoint',
description: 'Main production server with regional routing',
variables: [
'version' => new ServerVariableValue(
default: 'v1',
description: 'API version',
enum: ['v1', 'v2'],
),
],
);

Properties:

PropertyTypeRequiredDescription
namestringYesServer identifier
urlstringYesServer URL (supports variables)
summary?stringNoBrief description
description?stringNoDetailed description
variables?array<string, ServerVariableValue>NoURL template variables

Defines an error response following JSON-RPC 2.0 error format.

use Cline\OpenRpc\ValueObject\ErrorValue;
$error = new ErrorValue(
code: -32602,
message: 'Invalid params',
data: [
'field' => 'email',
'reason' => 'Invalid email format',
],
);

Properties:

PropertyTypeRequiredDescription
codeintYesNumeric error code
messagestringYesHuman-readable message
datamixedNoAdditional error data

Standard JSON-RPC 2.0 Error Codes:

CodeMessageDescription
-32700Parse errorInvalid JSON
-32600Invalid RequestInvalid JSON-RPC request
-32601Method not foundMethod does not exist
-32602Invalid paramsInvalid method parameters
-32603Internal errorServer error
-32000 to -32099Server errorReserved for implementation

Container for reusable specification components.

use Cline\OpenRpc\ValueObject\ComponentsValue;
use Cline\OpenRpc\ValueObject\SchemaValue;
$components = new ComponentsValue(
contentDescriptors: [
'UserId' => new ContentDescriptorValue(...),
],
schemas: [
'User' => new SchemaValue(
name: 'User',
data: [
'type' => 'object',
'required' => ['id', 'email'],
'properties' => [
'id' => ['type' => 'integer'],
'email' => ['type' => 'string', 'format' => 'email'],
'name' => ['type' => 'string'],
],
],
),
],
examples: null,
links: null,
errors: [
'NotFound' => new ErrorValue(
code: 404,
message: 'Resource not found',
data: null,
),
],
examplePairingObjects: null,
tags: null,
);

Properties:

PropertyTypeDescription
contentDescriptors?array<string, ContentDescriptorValue>Reusable parameter/result descriptors
schemas?array<string, SchemaValue>JSON Schema definitions
examples?array<string, ExampleValue>Reusable examples
links?array<string, LinkValue>Reusable links
errors?array<string, ErrorValue>Reusable error definitions
examplePairingObjects?array<string, ExamplePairingValue>Reusable example pairs
tags?array<string, TagValue>Reusable tags

Named JSON Schema definition for use in components.

use Cline\OpenRpc\ValueObject\SchemaValue;
$schema = new SchemaValue(
name: 'Pagination',
data: [
'type' => 'object',
'properties' => [
'page' => ['type' => 'integer', 'minimum' => 1],
'perPage' => ['type' => 'integer', 'minimum' => 1, 'maximum' => 100],
],
],
);
// Converts to: { "Pagination": { "type": "object", ... } }
$array = $schema->toArray();

Sample values for documentation and testing.

use Cline\OpenRpc\ValueObject\ExampleValue;
$example = new ExampleValue(
name: 'valid-user',
summary: 'A valid user object',
description: 'Example of a complete user with all fields',
value: json_encode([
'id' => 123,
'email' => 'user@example.com',
'name' => 'John Doe',
]),
externalValue: null,
);

Groups parameter examples with their corresponding results.

use Cline\OpenRpc\ValueObject\ExamplePairingValue;
use Cline\OpenRpc\ValueObject\ExampleValue;
use Spatie\LaravelData\DataCollection;
$pairing = new ExamplePairingValue(
name: 'get-user-by-id',
description: 'Get an existing user by their ID',
summary: 'Successful user retrieval',
params: new DataCollection(ExampleValue::class, [
new ExampleValue(
name: 'userId',
summary: null,
description: null,
value: '123',
externalValue: null,
),
]),
result: new DataCollection(ExampleValue::class, [
new ExampleValue(
name: 'user',
summary: null,
description: null,
value: json_encode(['id' => 123, 'name' => 'John']),
externalValue: null,
),
]),
);

Creates $ref pointers to reusable components.

use Cline\OpenRpc\ValueObject\ReferenceValue;
$ref = new ReferenceValue(
ref: '#/components/schemas/User',
);
$contact = new ContactValue(
name: 'API Support',
url: 'https://example.com/support',
email: 'api@example.com',
);
$license = new LicenseValue(
name: 'MIT',
url: 'https://opensource.org/licenses/MIT',
);
$tag = new TagValue(
name: 'users',
summary: 'User management methods',
description: 'Methods for creating, reading, updating, and deleting users',
externalDocs: null,
);
$link = new LinkValue(
name: 'GetUserById',
summary: 'Get the user by ID',
description: null,
method: 'user.get',
params: ['userId' => '$response.id'],
server: null,
);
$docs = new ExternalDocumentationValue(
description: 'Full API documentation',
url: 'https://docs.example.com/api',
);
$variable = new ServerVariableValue(
default: 'v1',
description: 'API version',
enum: ['v1', 'v2', 'beta'],
);