WordPress Application Passwords for secure REST API authentication
Application Passwords (WordPress 5.6+) let external scripts, apps and services authenticate to the REST API and XML-RPC without the user’s main password. Each one is a 24-character credential created per user and per application in Users → Profile → Application Passwords, shown in plain text only once and stored hashed. The profile lists every application password with its name, creation date, last use and last IP, and each one can be revoked on its own, without changing the main password or breaking the other integrations.
Problem: A Node.js inventory script updates WooCommerce stock through the REST API every 5 minutes with the shop manager’s main password from an .env file. When the shop manager changes the password, the sync silently stops, and the same password is copied to three servers without any rotation.
Solution: Create a dedicated user for the integration, give it an application password, and restrict that user to the WooCommerce product endpoints, so a leaked credential cannot be used for anything else.
How it authenticates
The client sends HTTP Basic authentication: the WordPress username and the application password (spaces in it are optional). Application Passwords are available only over HTTPS (or in a local development environment), so the credential is never sent in plain text. Sites that do not need them can switch them off completely with add_filter( 'wp_is_application_passwords_available', '__return_false' );.
Compared with the alternatives: cookie authentication with the wp_rest nonce is for JavaScript running on the site itself, in the admin or on the front end; Application Passwords are for server-to-server integrations and scripts that need stable credentials tied to one user; OAuth or JWT plugins are for third-party apps that act on behalf of many users.
A dedicated user and its password
With WP-CLI:
wp user create inventory-sync-bot sync-bot@example.com --role=shop_manager wp user application-password create inventory-sync-bot "ERP Inventory Sync" --porcelain
--porcelain prints only the password: copy it into the secrets store of the servers, it cannot be shown again. The same in PHP, for example in a setup routine:
<?php
$user_id = username_exists( 'inventory-sync-bot' );
if ( $user_id ) {
$result = WP_Application_Passwords::create_new_application_password( $user_id, array( 'name' => 'ERP Inventory Sync' ) );
if ( ! is_wp_error( $result ) ) {
list( $password, $item ) = $result;
// Show $password once to the admin; never write it to a log
}
}
Restrict the user to the product endpoints
<?php
add_filter( 'rest_pre_dispatch', function ( $result, $server, $request ) {
$user = wp_get_current_user();
if ( 'inventory-sync-bot' !== $user->user_login ) {
return $result;
}
if ( 0 !== strpos( $request->get_route(), '/wc/v3/products' ) ) {
return new WP_Error( 'rest_forbidden_route', 'This credential is restricted to product endpoints.', array( 'status' => 403 ) );
}
return $result;
}, 10, 3 );
$request->get_route() is the route after the REST prefix (/wc/v3/products/123), so the check does not depend on the URL structure or the rest_route query parameter.
The client
Node.js 18+ has fetch() built in:
const credentials = Buffer.from( `${ process.env.WP_USER }:${ process.env.WP_APP_PW }` ).toString( 'base64' );
async function updateProductStock( productId, stockQuantity ) {
const response = await fetch( `${ process.env.WP_URL }/wp-json/wc/v3/products/${ productId }`, {
method: 'PUT',
headers: {
Authorization: `Basic ${ credentials }`,
'Content-Type': 'application/json',
},
body: JSON.stringify( { manage_stock: true, stock_quantity: stockQuantity } ),
} );
if ( ! response.ok ) {
const error = await response.json().catch( () => ( {} ) );
throw new Error( `WC API ${ response.status }: ${ error.message || 'unknown error' }` );
}
return response.json();
}
Rotation and revocation
# List the credentials with their last use wp user application-password list inventory-sync-bot --fields=uuid,name,last_used,last_ip # Revoke one wp user application-password delete inventory-sync-bot <uuid> # Revoke all of the user's credentials wp user application-password delete inventory-sync-bot --all
To rotate: create a new password, deploy it, check last_used of the new one, then delete the old one.
NOTE: An application password acts with all the rights of its user. A shop manager can read every order and create coupons, so the route restriction above is an extra layer, not a replacement for the smallest suitable role. Application passwords do not work on the login form, only for API requests, so a service account that has only them cannot be used to log into the dashboard. Two-factor authentication of the login form does not apply to them either, see TOTP two-factor authentication.