Skip to main content

A typed Python client for the NEAR JSON-RPC API with Pydantic models and async HTTP support

Project description

NEAR JSON-RPC Python Client

Build Status License Python Type Safe Release Badge

A type-safe, Pythonic client for the NEAR Protocol JSON-RPC API.


Table of contents


📖 Overview

This library provides a type-safe, developer-friendly Python interface for interacting with the NEAR Protocol JSON-RPC API.

  • Fully typed request & response models
  • Clean separation between transport, RPC layer, and domain models
  • Designed for both scripting and production use

The client is inspired by official NEAR JSON-RPC client in Kotlin.

Module Description
client Python JSON-RPC client supporting both sync and async usage, with full NEAR RPC method wrappers (auto-generated)
models Typed Python classes for RPC requests and responses using Pydantic (auto-generated)
generator Tools for generating Python client and Pydantic models from NEAR’s OpenAPI specification

✨ Features

🎯 Type-Safe API All RPC requests and responses are represented as typed Python models (dataclasses / Pydantic), reducing runtime errors.

Simple & Explicit Design No magic. Each RPC method maps directly to a NEAR JSON-RPC endpoint.

🛡️ Structured Error Handling Clear distinction between:

  • JSON-RPC errors
  • HTTP errors
  • Network failures
  • Serialization issues

🔄 Sync & Async Friendly

  • Synchronous client for scripts & backend services using httpx.Client
  • Optional async client for asyncio-based applications using httpx.AsyncClient

📦 Minimal Dependencies Built on top of well-known Python libraries (httpx and pydantic).

🧪 Testable by Design Easy to mock transport layer for unit & integration tests.


⚙️ Requirements

  • Python 3.9+
  • httpx (used for both sync and async transports)
  • pydantic (for type-safe request/response models)

📦 Installation

pip install near-jsonrpc-client httpx pydantic

🚀 Quickstart

Async Client

import asyncio
from near_jsonrpc_client import NearClientAsync
from near_jsonrpc_models import RpcBlockRequest, BlockId, RpcBlockRequestBlockId, BlockIdBlockHeight


async def main():
    client = NearClientAsync(base_url="https://rpc.mainnet.near.org")

    params = RpcBlockRequest(
        RpcBlockRequestBlockId(
            block_id=BlockId(BlockIdBlockHeight(178682261))
        )
    )

    block = await client.block(params=params)
    print(block)

    await client.close()


asyncio.run(main())

Sync Client

from near_jsonrpc_client import NearClientSync
from near_jsonrpc_models import RpcBlockRequest, BlockId, RpcBlockRequestBlockId, BlockIdBlockHeight

client = NearClientSync(base_url="https://rpc.mainnet.near.org")

params = RpcBlockRequest(
    RpcBlockRequestBlockId(
        block_id=BlockId(BlockIdBlockHeight(178682261))
    )
)

block = client.block(params=params)
print(block)

client.close()

📝 Basic Usage

  • Create request models for each RPC method.
  • Call the method on the appropriate client (async or sync).
  • Receive typed response models.
from near_jsonrpc_models import RpcBlockRequest, RpcBlockRequestBlockId, BlockIdBlockHeight, BlockId

params = RpcBlockRequest(RpcBlockRequestBlockId(block_id=BlockId(BlockIdBlockHeight(178682261))))
response = client.block(params=params)
print(response)

⚠️ Handling Responses & Errors

The client raises structured exceptions:

  • RpcError – returned from NEAR JSON-RPC
  • HttpError – HTTP errors with status code and body
  • RequestTimeoutError – request timeout
  • ClientError – unexpected or invalid responses

Example:

from near_jsonrpc_client import RpcError, HttpError, RequestTimeoutError, ClientError

try:
    block = client.block(params=params)
except RpcError as e:
    print(f"RPC error: {e.error}")
except HttpError as e:
    print(f"HTTP error: {e.status_code}, {e.body}")
except RequestTimeoutError as e:
    print("Request timed out")
except ClientError as e:
    print("Invalid response", e)

🧪 Testing

  • Simply run pytest to execute all tests.
  • The transport layer (HttpTransportAsync or HttpTransportSync) is mocked internally, so no actual network calls are made.

🤝 Contributing

  • Fork the repository
  • Create a feature branch
  • Submit a pull request with tests

📜 License

This project is licensed under the Apache-2.0 License. See LICENSE for details.


📦 Deployment Guide

For detailed instructions on project structure, CI/CD workflow, versioning, and deployment steps, see the DEPLOYMENT.md file.


📚 References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

near_jsonrpc_client-1.0.15.tar.gz (122.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

near_jsonrpc_client-1.0.15-py3-none-any.whl (226.3 kB view details)

Uploaded Python 3

File details

Details for the file near_jsonrpc_client-1.0.15.tar.gz.

File metadata

  • Download URL: near_jsonrpc_client-1.0.15.tar.gz
  • Upload date:
  • Size: 122.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for near_jsonrpc_client-1.0.15.tar.gz
Algorithm Hash digest
SHA256 b8261d1c2e0e3939f60ab16dd0f83852f92b932a6cf31c3aa986c8f2f479d727
MD5 e70e7a0722c7fadabd3e36a55250a710
BLAKE2b-256 0c007d49aecd690a1bce747d09f66575d721bb2c5fd82c185c4dca27dab98acf

See more details on using hashes here.

File details

Details for the file near_jsonrpc_client-1.0.15-py3-none-any.whl.

File metadata

File hashes

Hashes for near_jsonrpc_client-1.0.15-py3-none-any.whl
Algorithm Hash digest
SHA256 6ed6c30ceeffc6d50291b291b1a4812eefdbfc4f53f8112379c60d24d30b8f67
MD5 b43a6d6e2cdb7c49f5005d64502b3aa5
BLAKE2b-256 265b59b5a10115b9f8f1e5cd99951eb8dbf12a60f3a1019b8c95b7a8f2c7c2bb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page