Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
"pages": [
"ecosystem/rpc/toncenter/overview",
"ecosystem/rpc/toncenter/get-api-key",
"ecosystem/rpc/toncenter/self-hosted-ton-center-v2",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reduce the name of the file. It's displayed in this way in a URL. ecosystem/rpc/toncenter/v2/self-host would probably be fine.

{
"group": "API v2",
"openapi": {
Expand Down
289 changes: 289 additions & 0 deletions ecosystem/rpc/toncenter/self-hosted-ton-center-v2.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
---
title: Self-Host API v2 (HTTP)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the article is only for v2 API, it should go into v2 API folder.

As far as I know, there are no non-HTTP JSON RPC provided by Toncenter, so (HTTP) seems extraneous.

---

## Overview
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't start an article with 2 headers in a row.

The part without a header is an overview.


This guide explains how to self-host the TON HTTP API (v2) locally using either:

- **Docker (recommended for production setups):** Works on any x86\_64 and arm64 OS with Docker available.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too much bold text.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List items have a structure: name, purpose, platforms. Such lists are best formatted as tables.

Platform list should be a list.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docker is a containerization solution. Python is a programming language. They shouldn't be listed as equivalent solutions.

It's very likely that inside of a Docker image there is the very same Python code.

- **Python (lightweight, for testing only):** Works on Ubuntu Linux (x86\_64, arm64), MacOSX (Intel x86\_64, Apple M1 arm64) and Windows (x86\_64).

## Recommended hardware

- **CPU architecture:** x86\_64 or arm64.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bold.

- **HTTP API only:** 1 vCPU, 2 GB RAM.
- **HTTP API with cache enabled:** 2 vCPUs, 4 GB RAM.

## Option 1: Run via Docker (recommended)

### Prerequisites

Before you begin, make sure the following tools are installed on your system:

- **Docker**
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bold.

- **Docker Compose** (preferably version 2 or higher)
- **curl**

<Accordion
title="How to install prerequisites"
>
* **macOS and Windows**:\
Download and install [Docker Desktop](https://www.docker.com/products/docker-desktop), which includes both Docker and Docker Compose.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about curl?


* **Ubuntu/Linux**:\
Navigate to the root of the repository and run the setup script:

```bash
scripts/setup.sh
```
</Accordion>

### 1. Clone the repository

```bash
git clone https://github.com/toncenter/ton-http-api.git
cd ton-http-api
```

### 2. Download TON config files

```bash
mkdir private
curl -sL https://ton-blockchain.github.io/global.config.json > private/mainnet.json
curl -sL https://ton-blockchain.github.io/testnet-global.config.json > private/testnet.json
```

### 3. Generate the `.env` file

Choose the network you want to use:

- **Testnet**:

```bash
export TON_API_TONLIB_LITESERVER_CONFIG=private/testnet.json
./configure.py
```

- **Mainnet**:

```bash
export TON_API_TONLIB_LITESERVER_CONFIG=private/mainnet.json
./configure.py
```

<Note>
The generated `.env` file includes default configuration options.\
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't break lines with \.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it's probably better to join those sentences in one paragraph.

That said, prefer <br /> to \

To customize settings or see all available environment variables, check the [Configuration](#configuration) section.
</Note>

### 4. Build and run the container

```bash
docker-compose build
docker-compose up -d
```

<Tip>Run `docker-compose down` to stop the container.</Tip>

### Test your API

Check masterchain info:

```bash
curl http://localhost/getMasterchainInfo
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should only work if port 80 is exposed from the container. On most *nix systems, this would (unnecessarily) require admin privileges.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Windows, localhost might default to ::1 IPv6 instead of 127.0.0.1 IPv4.

Which IP address API is set up to listen on? If it's not both, this might fail on some systems.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Furthermore, localhost might not always equal to 127.0.0.1. And 127.0.0.1 does not require a DNS resolution, so prefer using it explicitly.

```

Query a specific address:

```bash
curl -X GET \
'http://localhost/getAddressInformation?address=Uf_mlXHnufWO3-vvopflR_NpIFMiidvp_xt20Qf8usMBBPEE' \
-H 'accept: application/json'
```

<Tip>You can also make API calls via the Swagger interface on `http://localhost:80`</Tip>

### Switch between Mainnet and Testnet

To switch networks:

```bash
export TON_API_TONLIB_LITESERVER_CONFIG=private/mainnet.json # or private/testnet.json
./configure.py
docker-compose down
docker-compose up -d
```

This updates the `.env` file and restarts the container with the selected network config.

### Video walkthrough

[▶️ Watch Guide](https://www.canva.com/design/DAGuzMRoHjI/7FaLbvC29bSmS_JFykiu2g/watch?utm_content=DAGuzMRoHjI\&utm_campaign=designshare\&utm_medium=link2\&utm_source=uniquelinks\&utlId=h72f6e944b5)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider feedback from AI review above.


---

## Option 2: Run with Python (testing only)

### Prerequisites

Before you begin, make sure the following tools are installed on your system:

- **Python 3.8+**
- **pip**
- **curl**

<Accordion
title="How to install prerequisites"
>
* **macOS and Windows**:\
Python is usually pre-installed. If not, download it from [python.org](https://www.python.org/downloads/).\
Copy link
Collaborator

@verytactical verytactical Oct 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python is definitely not preinstalled on Windows.

Also, Python 2 and 3 are completely different programming languages.

Then, install the package:

```bash
pip install ton-http-api
```

* **Ubuntu/Linux**:\
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bold.

Install Python and pip if needed:

```bash
sudo apt update
sudo apt install python3 python3-pip
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might already be a different version of Python in PATH. After such installation procedure, there should be a step to check Python install is correct.

pip3 install ton-http-api
```
</Accordion>

### 1. Install the package

```bash
pip install ton-http-api
```

### 2. Run the service

- Default (Mainnet):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent with - Testnet:


```bash
ton-http-api
```

- Testnet:

```bash
ton-http-api --liteserver-config https://ton.org/testnet-global.config.json
```

or

```bash
TON_API_TONLIB_LITESERVER_CONFIG=https://ton.org/testnet-global.config.json ton-http-api
```

<Note> Run `ton-http-api --help` to show the list of available options and commands or check the [configuration](#configuration) section. </Note>

### Test your API

Check masterchain info:

```bash
curl http://localhost/getMasterchainInfo
```

Query a specific address:

```bash
curl -X GET \
'http://localhost/getAddressInformation?address=Uf_mlXHnufWO3-vvopflR_NpIFMiidvp_xt20Qf8usMBBPEE' \
-H 'accept: application/json'
```

<Tip>You can also make API calls via the Swagger interface on `http://localhost:80`</Tip>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http://localhost:80 is the same thing as http://localhost, because HTTP uses port 80 by default.

Is there really an interface there?

Is it really Swagger and not OpenAPI?


### Video walkthrough

[▶️ Watch Guide](https://www.canva.com/design/DAGuygDmwKk/TxCNWeDg4QjdTUSKkhAa2A/watch?utm_content=DAGuygDmwKk\&utm_campaign=designshare\&utm_medium=link2\&utm_source=uniquelinks\&utlId=hed091a20b9)

## Configuration
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a reference section that should go into a separate article. "How do I self-host this" how-to guide shouldn't force user to read over 100 lines of parameter reference.


TON HTTP API can be customized through environment variables (used by Docker and also respected by Python) or command-line flags (Python only).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most tools use environment variables. Docker indeed uses them, but its own different set of them.

I suspect merely setting them in a shell where Docker commands are executed won't make them available inside of the image.

Please describe where exactly they should be used.


<Accordion
title="Docker Environment Variables"
>
### Webserver

| Variable | Default | Description |
| ----------------------------- | --------- | --------------------------------------------------------------------- |
| `TON_API_HTTP_PORT` | `80` | HTTP port for the API service |
| `TON_API_ROOT_PATH` | `/` | Prefix path for reverse proxies (e.g., `/api/v2`) |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverse proxies are used to provide the path on their own. This is just a URL prefix for the API, and probably is not related to proxies.

Please check and expand this.

| `TON_API_WEBSERVERS_WORKERS` | `1` | Number of Gunicorn workers (set to \~CPU cores / 2) |
| `TON_API_GET_METHODS_ENABLED` | `1` | Enables the `runGetMethod` endpoint |
| `TON_API_JSON_RPC_ENABLED` | `1` | Enables the JSON-RPC endpoint |
| `TON_API_LOGS_JSONIFY` | `0` | Print logs in JSON format |
| `TON_API_LOGS_LEVEL` | `ERROR` | Logging verbosity: `DEBUG`, `INFO`, `WARNING`, `ERROR`, or `CRITICAL` |
| `TON_API_GUNICORN_FLAGS` | _(empty)_ | Extra Gunicorn CLI flags |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's Gunicorn?


### Tonlib

| Variable | Default | Description |
| ------------------------------------------------- | ------------------------------- | --------------------------------------- |
| `TON_API_TONLIB_LITESERVER_CONFIG` | `private/mainnet.json` (Docker) | Path to LiteServer config file |
| `TON_API_TONLIB_KEYSTORE` | `/tmp/ton_keystore` | Directory for tonlib keystore |
| `TON_API_TONLIB_PARALLEL_REQUESTS_PER_LITESERVER` | `50` | Max parallel requests per LiteServer |
| `TON_API_TONLIB_CDLL_PATH` | _(empty)_ | Custom path to `tonlibjson` binary |
| `TON_API_TONLIB_REQUEST_TIMEOUT` | `10` | Timeout in seconds for LiteServer calls |

### Cache

| Variable | Default | Description |
| ------------------------------ | ------------- | ------------------------------------ |
| `TON_API_CACHE_ENABLED` | `0` | Enables Redis caching |
| `TON_API_CACHE_REDIS_ENDPOINT` | `cache_redis` | Redis host (e.g., in Docker Compose) |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "e.g., in Docker Compose" part mean?

| `TON_API_CACHE_REDIS_PORT` | `6379` | Redis port |
| `TON_API_CACHE_REDIS_TIMEOUT` | `1` | Redis timeout in seconds |
</Accordion>

<Accordion
title="Python Command-Line Flags"
>
### Webserver

| Flag | Description |
| ------------------ | --------------------------------------------- |
| `--host` | Host address to bind the HTTP API |
| `--port` | HTTP port (equivalent to `TON_API_HTTP_PORT`) |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there an equivalent for --host parameter too? Why these are inconsistent between rows of the table?

| `--root` | Root path prefix for endpoints |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand what "Root path prefix for endpoints" does.

| `--no-get-methods` | Disables `runGetMethod` endpoint |
| `--no-json-rpc` | Disables JSON-RPC endpoint |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please link to JSON-RPC if it's the standard that was meant here.

Also, how do I call it?


### Tonlib

| Flag | Description |
| ------------------------------------ | -------------------------------------- |
| `--liteserver-config` | Path or URL to LiteServer config file |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would it distinguish /foo *nix path from /foo relative URL?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's LiteServer?

| `--tonlib-keystore` | Path to keystore directory |
| `--parallel-requests-per-liteserver` | Max concurrent requests per LiteServer |
| `--cdll-path` | Path to `tonlibjson` binary |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's tonlibjson?


### Cache

| Flag | Description |
| ------------------------ | --------------------- |
| `--cache` | Enables Redis caching |
| `--cache-redis-endpoint` | Redis host |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tables above provide default values for redis host/port. Are defaults the same here?

| `--cache-redis-port` | Redis port |

### Logs

| Flag | Description |
| ---------------- | ----------------------------------------------- |
| `--logs-level` | Logging level: `DEBUG`, `INFO`, `WARNING`, etc. |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any documentation about the things that might be found in logs?

| `--logs-jsonify` | Print logs in JSON format |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a schema for this JSON?


### Other

| Flag | Description |
| ----------- | ---------------------------------------- |
| `--version` | Show the current version of the CLI tool |
</Accordion>