How to Secure Your WordPress REST API Against Abuse
WordPress ships with the REST API turned on, and most site owners never think about it until something goes wrong. To secure WordPress REST API access, you need to know what it exposes, who can reach it without logging in, and which requests your site actually needs. The API powers the block editor, many plugins, and headless front ends, so the goal is not to switch it off blindly. Close the parts attackers use while the editor and the plugins you rely on keep working.
Start with a quick check. Logged out, open /wp-json/wp/v2/users on your site. If that route lists usernames, that information is public. Fix that first.
What the REST API Exposes and Why Attackers Care
The REST API gives outside software a structured way to read and, with the right credentials, change WordPress content. Anonymous visitors can read published posts, pages, categories, and media by default. That is normal for a public blog. The problem starts with routes that leak more than content.
The users endpoint is the best-known example. It can reveal usernames and display names. An attacker feeds those usernames into a password attack against /wp-login.php. Automated scanners probe /wp-json/ on thousands of sites a day, so a default setup is not hidden.
Scrapers also pull your full post archive through the API, and bots hammer it to test credentials. Each request costs CPU, database queries, and bandwidth. On a small VPS, that load adds up.
How to Secure WordPress REST API Access for Anonymous Users
The simplest control is to require authentication for API requests that are not part of normal public reading. WordPress provides the rest_authentication_errors filter for this. Add the code below to a small custom plugin or your child theme, and test it before you deploy it to a live site.
add_filter( 'rest_authentication_errors', function( $result ) {
if ( ! empty( $result ) ) return $result;
if ( ! is_user_logged_in() ) {
return new WP_Error( 'rest_not_logged_in', 'You are not logged in.', array( 'status' => 401 ) );
}
return $result;
} );
This blocks all anonymous API access, including public post reads. That is too strict for a site that serves posts to a mobile app or a headless front end. In that case, block only the routes you do not need, and leave public post reads alone. If your site has no app or headless front end, the strict version above is a reasonable choice.
Before you change anything, list the routes your site actually uses by checking your theme and plugins and watching your access log. A change that breaks a contact form or a mobile app will cost more than the risk it removes. See the WordPress security basics every VPS owner should work through first.
WordPress REST API Authentication: Cookies, Application Passwords, and JWT
WordPress REST API authentication works in three main ways. Cookie authentication is what the block editor uses. When you are logged in to /wp-admin/, your browser sends a cookie with each API request, and WordPress checks the nonce that comes with it. No extra credential is stored in your code.
Application passwords are the right choice for an external script, a mobile app, or a service that calls the API on your behalf. WordPress has supported them since version 5.6. Go to Users, open a profile, and generate one under Application Passwords. Each password is tied to one user, can be revoked on its own, and only works over HTTPS. Give the script the lowest-privilege user that can do the job. If a password leaks, revoke that one password.
JWT authentication is a third option, handled by a plugin such as JWT Authentication for WP REST API. A client sends a username and password once, receives a token, and sends that token with later requests. It suits headless setups that cannot use cookies. Keep the plugin updated, store the secret key outside version control, and set a short token lifetime. Never put a username and password in a URL, a JavaScript file served to visitors, or a public repository.
Block WordPress User Enumeration Through /wp-json/wp/v2/users
User enumeration is the most common REST API abuse, and it has a direct fix. Remove the users route for anonymous visitors while leaving post reads alone, using the rest_endpoints filter below.
add_filter( 'rest_endpoints', function( $endpoints ) {
if ( ! is_user_logged_in() ) {
foreach ( $endpoints as $route => $details ) {
if ( 0 === strpos( $route, '/wp/v2/users' ) ) unset( $endpoints[ $route ] );
}
}
return $endpoints;
} );
Test it logged out. A request to /wp-json/wp/v2/users should return a 404, while /wp-json/wp/v2/posts still returns your published posts. Logged-in users keep normal access.
Removing the route is only one layer. WordPress also exposes author names on archive pages and in post feeds. Fix the API route first, then check the other leaks. The steps in how to protect your WordPress login page from brute force attacks close the other half of that loop, and the guide to disable XML-RPC to stop wp-login brute force attacks covers the older endpoint.
WordPress REST API Rate Limiting with Nginx and Fail2ban
Authentication decides who can call the API. Rate limiting decides how often. A bot that gets a 401 on every request is still a problem if it makes ten thousand requests an hour. WordPress has no built-in rate limit for the REST API, so this belongs at the server level.
If Nginx sits in front of WordPress, use limit_req. Define a zone once, then apply it to the API location. The example below allows about five requests per second per IP, with a short burst.
limit_req_zone $binary_remote_addr zone=wpjson:10m rate=5r/s;
location ~ ^/wp-json/ {
limit_req zone=wpjson burst=10 nodelay;
limit_req_status 429;
# your existing proxy_pass or fastcgi_pass goes here
}
Adapt the location block to your own setup, keeping your existing fastcgi_pass or proxy_pass lines inside it. After a reload, confirm you get a 429 when you exceed the burst, then check that the editor still saves posts. For a fuller walkthrough, see how to rate limit Nginx to stop brute force and scraping bots.
Fail2ban adds a second layer. Create a filter that matches repeated 401 or 429 responses on /wp-json/ in your Nginx access log, then add a jail that bans the IP after a few matches in a short window. Start with a ten-minute ban and watch the log for a day before you lengthen it.
Keep the Block Editor Working While You Lock Things Down
The block editor is the part most people break when they harden the API. It saves posts, loads blocks, fetches media, and checks autosaves through /wp-json/wp/v2/ routes, all with cookie authentication. If you block anonymous API access, the editor still works, because you are logged in when you edit. The risk is a plugin, a caching layer, or a server rule that blocks the API for everyone, including logged-in users. A post that will not save is often a sign that a route was blocked too broadly.
Some plugins depend on specific routes. A form plugin may submit through the API, and a membership plugin may check the current user through /wp/v2/users/me. Before you remove a route, search the plugin code for wp-json and register_rest_route. A custom route with no permission callback is open to anonymous visitors by default.
Test every change logged in and logged out. Save a draft, publish a test post, and submit each form. If a plugin breaks, narrow the rule instead of removing the whole control.
A Practical Checklist You Can Apply Today
Work through these in order, testing after each step. First, open /wp-json/wp/v2/users logged out and check whether usernames are visible. Second, remove that route for anonymous visitors. Third, revoke shared or unused application passwords, and replace administrator-level API credentials with a lower-privilege user. Fourth, add the Nginx rate limit and confirm the editor still saves. Fifth, add a Fail2ban jail for repeated 401 and 429 responses.
Then look at the rest of the server. A locked API on a loose server is a partial fix. Make sure wp-config.php is not readable from the web and file permissions are set the way they should be, as described in the guide to secure wp-config.php and set WordPress file permissions. Check your logs once a week for new /wp-json/ routes you do not recognise.