Developers connecting code to the WordPress REST API have four ways to sign in, and the right one depends on who is calling. For JavaScript running inside your own site, use the login cookie that is already there, plus a nonce, a short-lived token WordPress uses to confirm the request came from its own pages. For scripts, servers and most integrations, use application passwords, which have been built into WordPress since 5.6. Add a JWT or OAuth plugin only when a separate app has to sign in many different users, or needs permission scopes that core does not offer. All of them end at the same place: WordPress sets a current user and checks that user’s permissions as it would in the dashboard.
What you’ll accomplish
- Understand how each method carries proof of identity to the REST API, and where on the request path it can be lost.
- Compare cookie and nonce authentication, application passwords, JWT plugins and OAuth plugins on the criteria that matter in production.
- Check from the command line which methods your site accepts today, and read the exact error each one returns when it fails.
- Pick one method per kind of client, and move an existing integration onto it without downtime.
Prerequisites
- WordPress 6.x on PHP 8.1 or later. Application passwords need WordPress 5.6 or later.
- The site served over HTTPS. WordPress hides application passwords on sites without it, except when the environment type is set to
local. curlandjq(a command-line JSON viewer) on your workstation, plus SSH with WP-CLI for the server-side checks.- A dedicated WordPress user for each integration, with the lowest role that can do the job. Every method here acts with the full permissions of the user it signs in as.
How each method works
Cookie and nonce. When you are logged in, your browser holds WordPress’s login cookie. Cookies are sent automatically, so on their own they would let any other website trigger actions in your name, an attack known as cross-site request forgery. WordPress therefore ignores the cookie on REST requests unless the request also carries a nonce in the X-WP-Nonce header. The nonce is generated for the wp_rest action and stays valid for 12 to 24 hours. Without the header, the request is treated as logged out. With a wrong or expired one, WordPress returns 403. The block editor and wp.apiFetch add the header for you; your own scripts get the value from wp_create_nonce( 'wp_rest' ).
Application passwords. Each user can create any number of 24-character passwords, one per app, from their profile or with WP-CLI. The app sends them with HTTP Basic authentication: an Authorization header holding the username and the application password. They only work for API requests, never for the login form, so a leaked one cannot open the dashboard. WordPress stores them hashed, records when and from which IP each was last used, and lets you revoke one without touching the others. They carry no scopes, the per-key limits some APIs offer, so a key can do everything its user can.
JWT plugins. JWT (JSON Web Token) is a signed token format. A plugin adds an endpoint where the app posts a username and the real password once and receives a token, which it then sends as Authorization: Bearer <token>. On each request the plugin checks the signature against a secret kept in wp-config.php, without storing the token anywhere. That makes tokens fast to verify and hard to revoke: unless the plugin keeps its own list, a stolen token works until it expires, and changing the secret logs out every client at once.
OAuth plugins. OAuth 2.0 lets a user approve an app in the browser without ever giving the app their password. The app receives an access token, and often a refresh token, limited to the scopes the user approved. It is the model to use when third-party apps you do not control connect on behalf of many users. WordPress core does not include an OAuth server, so you depend on a plugin for the flow, the scopes and the token storage.
The two header-based paths share a weak point. On some setups, typically Apache running PHP as CGI or FastCGI, the web server does not pass the Authorization header to PHP. WordPress then sees an anonymous request and answers 401, even with a perfect password.
The criteria compared
| Criterion | Cookie and nonce | Application passwords | JWT plugin | OAuth 2.0 plugin |
|---|---|---|---|---|
| Built into core | Yes | Yes (5.6+) | No | No |
| Typical caller | JavaScript on your own site | Scripts, servers, desktop and mobile tools | Single-page and mobile apps you build | Third-party apps acting for many users |
| What travels with each request | Cookie plus nonce header | Username and app password | Signed bearer token | Access token |
| Real password given to the app | No | No | Yes, once, to get a token | No |
| Revoke one client | Log the user out | Delete that one app password | Depends on the plugin | Revoke that token or app |
| Limit what it can do | User’s role only | User’s role only | User’s role, unless the plugin adds more | Scopes, as defined by the plugin |
| Breaks when | Page cache serves a stale nonce | Server strips the Authorization header | Same header problem, or the token expires | Plugin or token storage misconfigured |
Check which methods your site accepts
1. Read what the API index advertises. WordPress lists its built-in authentication options at the root of the API:
curl -s https://example.com/wp-json/ | jq '.authentication'
{
"application-passwords": {
"endpoints": {
"authorization": "https://example.com/wp-admin/authorize-application.php"
}
}
}
If you get [] instead, application passwords are switched off: the site is not on HTTPS, or a plugin turned them off through the wp_is_application_passwords_available filter. Confirm on the server with wp --url=https://example.com eval 'var_dump( wp_is_application_passwords_available() );', which should print bool(true). Keep the --url with https://: without it, WP-CLI does not know the site uses HTTPS and prints bool(false) even on a healthy site.
2. Establish the anonymous baseline. /wp/v2/users/me returns the current user, so it shows quickly whether authentication worked:
curl -s https://example.com/wp-json/wp/v2/users/me
{"code":"rest_not_logged_in","message":"You are not currently logged in.","data":{"status":401}}
3. Test an application password end to end. Create one for a dedicated user. --porcelain prints only the password, which WordPress never shows again:
wp user application-password create api-bot "Inventory sync" --porcelain
Kx3PqR7tYb2mWn9vZc4LsH8d
Example output. Call the API with it:
curl -s -u 'api-bot:Kx3PqR7tYb2mWn9vZc4LsH8d' https://example.com/wp-json/wp/v2/users/me | jq '{id, name}'
{
"id": 7,
"name": "api-bot"
}
If you get rest_not_logged_in here, the password never reached WordPress, and the Authorization header is being stripped (see Troubleshooting). If you get incorrect_password or invalid_username, it reached WordPress and was rejected.
4. Test cookie and nonce from the browser. Log in, open any post in the block editor, open the browser’s developer console and run:
await wp.apiFetch( { path: '/wp/v2/users/me' } );
The console shows your own user object, because wp.apiFetch added the nonce. For your own front-end scripts, pass the nonce and the API root from PHP:
wp_enqueue_script( 'my-app', get_stylesheet_directory_uri() . '/js/my-app.js', array(), '1.0', true );
wp_localize_script( 'my-app', 'myApp', array(
'root' => esc_url_raw( rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' ),
) );
Then send it with each request:
fetch( myApp.root + 'wp/v2/users/me', {
headers: { 'X-WP-Nonce': myApp.nonce },
credentials: 'same-origin',
} );
Who should pick which
Pick cookie and nonce when the code runs in pages your WordPress site serves to logged-in users: admin screens, block editor extensions, a front-end dashboard for members. Nothing to store, nothing to revoke separately, and the browser handles the cookie.
Pick application passwords for everything that runs outside the browser and acts as one known account: a deployment script, an inventory sync, a reporting job, a mobile publishing app, an automation platform. Create one user per integration and one password per app, and you can see each one’s last use and revoke it alone. For an app that should connect at a user’s request, send the user to /wp-admin/authorize-application.php with an app_name and an HTTPS success_url; WordPress asks them to approve and returns a new application password to that URL. That covers many “connect your site” flows without any plugin.
Pick a JWT plugin when you build a separate front end, such as a mobile app, where many different site users sign in with their own username and password, and you control both ends. Keep token lifetimes short, store the secret outside version control, and check how the plugin handles logout and revocation before you rely on it.
Pick an OAuth plugin when third-party developers you do not control will build apps that act for your users, and those users need to see and approve what each app can do. That consent screen and its scopes are the reason to accept the extra plugin.
How to move between them
The most common move is retiring an old integration that sends a real password, through the legacy Basic Auth plugin or a JWT plugin, onto application passwords:
- Create a dedicated user with the lowest role that works, then create an application password for it as in step 3 above.
- Update the integration to send
-u user:app-password(Basic authentication) in place of the old header, and run its normal job once. - Confirm the new key is in use. The
last_usedcolumn fills in after the first request:
wp user application-password list api-bot --fields=uuid,name,last_used,last_ip
+--------------------------------------+----------------+------------+--------------+
| uuid | name | last_used | last_ip |
+--------------------------------------+----------------+------------+--------------+
| 6f1c2a4e-9b3d-4c8e-a2f7-1d5e8b0c3a96 | Inventory sync | 1793259027 | 203.0.113.48 |
+--------------------------------------+----------------+------------+--------------+
Example output. last_used is a Unix timestamp.
- Change the old user’s real password, since it was stored in the integration, then deactivate the old plugin.
- To rotate a key later, create the new password, switch the integration, wait for
last_usedto appear on it, then delete the old one withwp user application-password delete api-bot <uuid>.
Troubleshooting
rest_not_logged_in (401) even with the right application password. The Authorization header is lost before PHP. On Apache 2.4.13 or later, add CGIPassAuth On to the site’s .htaccess. On older setups, SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1 does the same. Also check any proxy or web application firewall in front of the site, since some strip the header too.
rest_cookie_invalid_nonce (403, “Cookie check failed”). The nonce is expired or was made for another action. The usual cause is a page cache serving a page, nonce included, that was generated 12 to 24 hours earlier. Exclude logged-in users from the page cache, or fetch a fresh nonce from admin-ajax.php?action=rest-nonce, which core provides for exactly this, before calling the API.
incorrect_password or invalid_username (401). The password reached WordPress but does not match an application password for that username, or the username does not exist. Check that you used the username, not the display name, and that the key was not revoked. The spaces WordPress shows in the password are optional.
rest_forbidden or rest_cannot_create (403). Authentication worked; the user lacks the capability for that action. Give the integration’s user a role that allows it, rather than switching it to an administrator. The REST API errors guide walks through these codes in more depth.
The Application Passwords section is missing from user profiles. The site is not on HTTPS, or a security plugin disabled the feature. Step 1 above tells you which.
When this is the wrong approach
If the integration only reads public posts, it needs no authentication, and adding some only creates a credential to protect. If your concern is anonymous visitors reading data such as the user list, that is about restricting unauthenticated access, which the guide to restricting the unauthenticated REST API covers. For a full walk-through of one integration on application passwords, see connecting an external app with application passwords. WooCommerce stores also have their own consumer keys, covered in the WooCommerce REST API authentication guide. The official REST API authentication handbook documents the core methods.
Running WordPress that earns real money deserves an operator — Stack Harbor runs that stack under Enterprise WordPress; book an infrastructure consult.