PHPackages                             o-perator/json-rpc - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. o-perator/json-rpc

ActiveLibrary[API Development](/categories/api)

o-perator/json-rpc
==================

a JSON-RPC 2.0 server

v4.1.0(3y ago)014MITPHPPHP ~8.0.0 || ~8.1.0 || ~8.2.0CI passing

Since Feb 13Pushed 1y agoCompare

[ Source](https://github.com/o-perator/JsonRpc)[ Packagist](https://packagist.org/packages/o-perator/json-rpc)[ RSS](/packages/o-perator-json-rpc/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (7)Versions (4)Used By (0)

JsonRpc
=======

[](#jsonrpc)

[![Build Status](https://github.com/1ma/JsonRpc/workflows/.github/workflows/phpunit.yml/badge.svg)](https://github.com/1ma/JsonRpc/actions)[![Code Coverage](https://camo.githubusercontent.com/7735c3b13b81baa39fb46526e68958d3c65338a0c94e0c8c1c127449aee03155/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f316d612f4a736f6e5270632f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/1ma/JsonRpc/?branch=master)

A modern, object-oriented [JSON-RPC 2.0](http://www.jsonrpc.org/specification) server for PHP 8.0+ featuring JSON Schema integration and middlewaring.

Table of Contents
=================

[](#table-of-contents)

- [Installation](#installation)
- [Basic Usage](#basic-usage)
    - [Creating Procedures](#creating-procedures)
    - [Registering Services](#registering-services)
    - [Running the Server](#running-the-server)
- [Custom Validation](#custom-validation)
- [Middlewares](#middlewares)
    - [Middleware guarantees](#middleware-guarantees)
    - [Middleware ordering](#middleware-ordering)
    - [Middleware example](#middleware-example)
- [FAQ](#faq)
    - [Does JSON-RPC 2.0 have any advantage over REST?](#does-json-rpc-20-have-any-advantage-over-rest)
    - [How can I attach a middleware to specific procedures instead of the whole Server?](#how-can-i-attach-a-middleware-to-specific-procedures-instead-of-the-whole-server)
    - [How do you integrate `uma/json-rpc` with other frameworks?](#how-do-you-integrate-umajson-rpc-with-other-frameworks)
- [Best Practices](#best-practices)
    - [Rely on JSON schemas to validate params](#rely-on-json-schemas-to-validate-params)
    - [Defer actual work whenever possible](#defer-actual-work-whenever-possible)
    - [Cap the number of batch requests](#cap-the-number-of-batch-requests)

Installation
------------

[](#installation)

```
$ composer require uma/json-rpc
```

Basic Usage
-----------

[](#basic-usage)

Setting up a `JsonRpc\Server` involves three separate steps: coding the procedures you need, registering them as services and finally configuring and running the server.

### Creating Procedures

[](#creating-procedures)

Procedures are akin to HTTP controllers in the MVC pattern, and must implement the `UMA\JsonRpc\Procedure` interface.

This example shows a possible implementation of the `subtract` procedure found in the JSON-RPC 2.0 specification examples:

```
declare(strict_types=1);

namespace Demo;

use stdClass;
use UMA\JsonRpc;

class Subtractor implements JsonRpc\Procedure
{
    /**
     * {@inheritdoc}
     */
    public function __invoke(JsonRpc\Request $request): JsonRpc\Response
    {
        $params = $request->params();

        if ($params instanceof stdClass) {
            $minuend = $params->minuend;
            $subtrahend = $params->subtrahend;
        } else {
            [$minuend, $subtrahend] = $params;
        }

        return new JsonRpc\Success($request->id(), $minuend - $subtrahend);
    }

    /**
     * {@inheritdoc}
     */
    public function getSpec(): ?stdClass
    {
        return \json_decode(set(Subtractor::class, function(): Subtractor {
    return new Subtractor();
});

$c->set(Server::class, function(Container $c): Server {
    $server = new Server($c);
    $server->set('subtract', Subtractor::class);

    return $server;
});
```

At this point we have a JSON-RPC server with one single method (`subtract`) that is mapped to the `Subtractor::class` service. Moreover, the procedure definition is lazy, so `Subtractor` won't be actually instantiated unless `subtract` is actually called in the server.

Arguably this is not very important in this example. But it'd be with tens of procedure definitions, each with their own dependency tree.

### Running the Server

[](#running-the-server)

Once set up, the same server can be run any number of times, and it will handle most errors defined in the JSON-RPC spec on behalf of the user of the library:

```
declare(strict_types=1);

use UMA\JsonRpc\Server;

$server = $c->get(Server::class);

// RPC call with positional parameters
$response = $server->run('{"jsonrpc":"2.0","method":"subtract","params":[2,3],"id":1}');
// $response is '{"jsonrpc":"2.0","result":-1,"id":1}'

// RPC call with named parameters
$response = $server->run('{"jsonrpc":"2.0","method":"subtract","params":{"minuend":2,"subtrahend":3},"id":1}');
// $response is '{"jsonrpc":"2.0","result":-1,"id":1}'

// Notification (request with no id)
$response = $server->run('{"jsonrpc":"2.0","method":"subtract","params":[2,3]}');
// $response is NULL

// RPC call with invalid params
$response = $server->run('{"jsonrpc":"2.0","method":"subtract","params":{"foo":"bar"},"id":1}');
// $response is '{"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params"},"id":1}'

// RPC call with invalid JSON
$response = $server->run('invalid input {?set('add', Adder::class);

$response = $server->run('[
  {"jsonrpc": "2.0", "method": "add", "params": [], "id": 1},
  {"jsonrpc": "2.0", "method": "add", "params": [1,2], "id": 2},
  {"jsonrpc": "2.0", "method": "add", "params": [1,2,3,4], "id": 3}
]');
// $response is '{"jsonrpc":"2.0","error":{"code":-32000,"message":"Too many batch requests sent to server","data":{"limit":2}},"id":null}'
```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 97.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~67 days

Total

2

Last Release

1121d ago

Major Versions

1.x-dev → v4.1.02023-04-21

PHP version history (2 changes)1.x-devPHP ~8.0.0 || ~8.1.0

v4.1.0PHP ~8.0.0 || ~8.1.0 || ~8.2.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/762681f1433b40dc47861001269695d20feadb3bab03ef58dcad4bc290db48c8?d=identicon)[o-perator](/maintainers/o-perator)

---

Top Contributors

[![1ma](https://avatars.githubusercontent.com/u/1456708?v=4)](https://github.com/1ma "1ma (106 commits)")[![astronom](https://avatars.githubusercontent.com/u/729063?v=4)](https://github.com/astronom "astronom (1 commits)")[![flameinthehead](https://avatars.githubusercontent.com/u/45467485?v=4)](https://github.com/flameinthehead "flameinthehead (1 commits)")[![o-perator](https://avatars.githubusercontent.com/u/51188759?v=4)](https://github.com/o-perator "o-perator (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/o-perator-json-rpc/health.svg)

```
[![Health](https://phpackages.com/badges/o-perator-json-rpc/health.svg)](https://phpackages.com/packages/o-perator-json-rpc)
```

###  Alternatives

[irazasyed/telegram-bot-sdk

The Unofficial Telegram Bot API PHP SDK

3.3k4.5M84](/packages/irazasyed-telegram-bot-sdk)[thecodingmachine/graphqlite

Write your GraphQL queries in simple to write controllers (using webonyx/graphql-php).

5723.1M30](/packages/thecodingmachine-graphqlite)[scriptfusion/porter

Scalable and durable data import for publishing and consuming APIs.

613138.9k14](/packages/scriptfusion-porter)[prooph/service-bus-symfony-bundle

88392.2k3](/packages/prooph-service-bus-symfony-bundle)[spiral/grpc-client

gRPC client

41140.3k2](/packages/spiral-grpc-client)[uma/json-rpc

a JSON-RPC 2.0 server

3756.6k2](/packages/uma-json-rpc)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
