The Xray REST API is a remote gRPC and HTTP interface built into the Xray-core proxy server, letting you query traffic statistics, adjust routing, and manage users without restarting the service. It achieves this by exposing an internal API service that answers on a local port. Before it works, you must enable the API in your Xray configuration, add an inbound that serves the gRPC handler, and give your client account the permissions to call it. The steps below use the standard Xray configuration file and the official Xray CLI binary; exact menu labels and flags can differ slightly between versions, so check your installed build's help output if a flag name looks different.
What You'll Need
- A working Xray-core installation, with the config file (usually
config.json) accessible and a service or process you can restart. - Command-line access to the machine running Xray (SSH is fine for a remote server).
- The Xray binary itself on the same host, because the API client is a subcommand of the
xrayexecutable. - A free local port for the API, plus HTTPS or a private network if you intend to reach it remotely.
- Optional: a way to open an SSH tunnel (for example
ssh -L) if you want to call the API from your own computer.
Step-by-Step
- Check that your Xray binary includes the API. Run
xray api -h. A help screen listing subcommands such asstats,statsonline,restartlogger,adduser,rmuserandinboundusermeans you are ready. Recent official builds bundle the API commands; older or custom builds may not, so reinstall from the official release if the subcommand is missing. - Back up your configuration. Copy
config.jsonsomewhere safe before editing. A syntax error in this file stops Xray from starting, and a backup lets you roll back in seconds. - Add the API routing rule. In the top-level
routingsection, add a rule that directs traffic for the API service tag to the built-in handler. The rule uses afieldwithinboundTagvalues for your API inbound and anoutboundTagofapi. Place this rule before any broad rules that would otherwise catch the traffic. - Declare the API service. In the top-level
apisection, settagtoapiand choose whether you want gRPC or an alternative transport. Setting the tag is what creates the internal service the routing rule sends traffic to. - Create a dedicated inbound for the API. Add an inbound with
protocolset todokodemo-door,listenon127.0.0.1, a port of your choice, and asettingsblock withaddressset to127.0.0.1. Give it the same tag you used in the routing rule. Binding to127.0.0.1is important: it keeps the API off the public internet, where an unauthenticated management interface would be a serious risk. - Turn on statistics. Add a top-level
statsobject with an empty value, then add apolicyblock whosesystemsection setsstatsInboundUplink,statsInboundDownlink,statsOutboundUplinkandstatsOutboundDownlinktotrue. Without these flags the API answers but every counter reads zero. - Add an API account. In the
apisection, anserviceslist can enable optional modules such as the stats service or the reflection service. If your version uses the reflection service for command discovery, enabling it makes the CLI easier to explore. - Validate the config without restarting. Run
xray run -test -config config.jsonor the equivalent test flag in your build. Fix any message about an unknown field or trailing comma before going further. - Restart Xray. With systemd that is
systemctl restart xrayor the unit name you configured. Then check the service status and the log for a line confirming the API inbound started and the API service is registered. - Query the API locally. On the server, run
xray api statsquery --server=127.0.0.1:PORT, replacingPORTwith your chosen port. You should get a list of statistics names. To read one counter, add-namefollowed by the statistic's exact name. - Read statistics for a specific inbound or user. The CLI's
statssubcommand with a-namesuch as an inbound uplink counter returns a number. Use the names returned by the query step rather than guessing the format; they follow a pattern likeinbound>>>tag>>>traffic>>>uplink, but the exact separators and case matter and differ by build. - Manage users dynamically. The
adduser,rmuserandinboundusersubcommands change a user's credentials in a running inbound without a restart. The changes live in memory, so they disappear on the next restart unless you also apply them to your config or your own user store. - Reach the API from your own machine, if needed. Keep the inbound on loopback and open an SSH tunnel:
ssh -L PORT:127.0.0.1:PORT user@server. Then point the CLI at127.0.0.1:PORTon your local machine. Never expose the API port directly to the internet.
Common Mistakes
The most frequent failure is a routing rule that never matches. If the inboundTag in the routing rule is not identical to the API inbound's tag, requests fall through to your default outbound and the CLI hangs or returns an empty reply. Copy the tag character for character.
A second common error is forgetting the stats block or the policy flags. The API responds, the CLI prints an empty list, and people assume the API is broken when statistics were simply never enabled.
Third, binding the API inbound to 0.0.0.0 instead of 127.0.0.1. This turns a management interface into a public one and, because Xray's API has no built-in password, exposes your configuration to anyone who finds the port.
Fourth, editing the config while the service is running and expecting changes to apply. Xray reads its configuration at start time; every structural change needs a restart, even when user-credential changes through the API do not.
Finally, using statsquery against the wrong port, such as the port of a proxy inbound rather than the API inbound. They are different ports with different protocols.
Tips
Use the CLI before writing any code. Every operation you can do through the API can usually be exercised from the command line, and once you have a working command you can translate it into a language client far more easily.
Name statistics in your configuration. By default, user counters may be disabled. If you want per-user totals, enable the user-level statistics flags in the policy block, then reference counters by user email.
Script a backup-and-apply routine: test the new config, restart, then make one API query to confirm the service came back. That single check catches most broken deployments immediately.
For dashboards, poll statistics at a sensible interval rather than in a tight loop, and store the previous values so you can calculate rates instead of raw cumulative totals.
Troubleshooting
The CLI hangs or returns a connection error. Confirm Xray is running, that the API inbound is listening on the expected port (check with ss -ltnp or your system's equivalent), and that the routing rule points at the right tag. A tunnel that was closed will also produce this symptom.
You get an empty statistics list. Check the policy flags for inbound and outbound statistics, and make sure traffic has actually passed through the monitored tags since the last restart.
Config test fails after editing. Look for an unknown field, a missing comma, or a rule placed in the wrong section. Restore your backup if you cannot spot it, then reapply the changes in small pieces.
Changes made through the API vanish after a restart. This is expected; the API edits runtime state only. Persist the change in your configuration or your own provisioning system.
The service starts but clients lose connectivity. A broad routing rule placed before the API rule, or a misplaced bracket that nested the API inbound inside another inbound, can break normal traffic. Re-test the config and review the rule order.
Frequently Asked Questions
Does the Xray API need a username and password?
No. It relies on network isolation, which is why the inbound should listen on 127.0.0.1 or sit behind a private network or tunnel.
Can I use the API over plain HTTP from another machine?
The API is served by an inbound you configure, so you can, but only over a trusted connection such as an SSH tunnel or VPN. Exposing it publicly is unsafe.
Do API changes survive a restart?
No. Runtime edits such as adding a user last until Xray restarts. Update the config file to make them permanent.
Why do my counters stay at zero?
The statistics flags in the policy block are off, or no traffic has passed through the tag you are querying.
Which clients can talk to the API?
The official CLI is simplest, and the service can also be called with a gRPC client generated from the official protocol definitions. Avoid unofficial wrappers unless you have reviewed them.
Can I change the API port later?
Yes. Edit the inbound port, update any tunnel and the CLI --server value, then restart Xray.