Authentication overview
The WordPress REST API provides programmatic access to your WordPress site's content and data, enabling developers to build custom front-ends, mobile applications, and integrations. To ensure secure interactions, authentication is required for most write operations (creating, updating, deleting) and for accessing private content. The API integrates directly into WordPress core, offering methods designed to fit various application types and security requirements.
Understanding the appropriate authentication method for your specific use case is crucial for maintaining data integrity and user privacy. WordPress offers built-in mechanisms that balance ease of use with necessary security measures, allowing developers to choose the best fit for their application's architecture and trust model.
Supported authentication methods
The WordPress REST API supports several authentication methods, each suitable for different scenarios. The primary approaches include Application Passwords and OAuth 1.0a. While cookie authentication is used for logged-in users within the WordPress admin, it is generally not recommended for external applications due to its reliance on browser sessions.
Application Passwords
Application Passwords provide a secure way for external applications to authenticate with the WordPress REST API without exposing a user's primary password. They function as unique, revocable credentials tied to a specific user account. Each application password can be given a descriptive name, making it easier to manage and revoke access if an application is compromised or no longer needed. This method is particularly well-suited for server-to-server communications, trusted desktop applications, or mobile apps where the user explicitly grants access.
- How it works: An application password is generated for a specific user. The external application then sends this password in the
Authorizationheader using Basic Authentication. - Use cases: headless WordPress setups, mobile applications, custom integrations, command-line tools.
- Security: High, as passwords can be individually revoked without affecting the user's primary login.
OAuth 1.0a
OAuth 1.0a is a protocol that allows third-party applications to obtain limited access to a user's resources on an HTTP service, without requiring the user to share their credentials with the third-party application. For the WordPress REST API, OAuth 1.0a is implemented via a plugin, typically the OAuth 1.0a Server plugin. This method is more complex to set up but provides a robust framework for delegated authorization, making it ideal for public-facing applications or services that need to interact with many different WordPress sites on behalf of their users.
- How it works: The user authorizes the third-party application, which then receives a token allowing it to make requests on behalf of the user. The process involves multiple steps: consumer key/secret, request token, user authorization, and access token exchange, as detailed in the OAuth 1.0a specification.
- Use cases: social media integrations, cross-platform publishing tools, services that require user consent to access their WordPress data.
- Security: Very high, designed for delegated access without sharing user credentials.
Cookie Authentication (for internal use)
Cookie authentication is the standard method for users logged into the WordPress admin area. When a user logs in, WordPress sets authentication cookies in their browser. These cookies are then automatically sent with subsequent requests to the REST API, authenticating the user. While effective for front-end interactions within the same domain as the WordPress installation, it is generally not suitable for external, third-party applications due to Cross-Origin Resource Sharing (CORS) restrictions and session management complexities.
Here's a summary of the authentication methods:
| Method | When to Use | Security Level |
|---|---|---|
| Application Passwords | Server-to-server, trusted clients, internal tools, mobile apps | High (revocable, user-specific) |
| OAuth 1.0a | Third-party applications, public services requiring delegated access | Very High (delegated, no credential sharing) |
| Cookie Authentication | Logged-in users within the WordPress admin (not for external apps) | Standard (browser session-based) |
Getting your credentials
For Application Passwords
To generate an Application Password:
- Log in to your WordPress admin dashboard.
- Navigate to Users > Profile.
- Scroll down to the Application Passwords section.
- Enter a new name for your application password (e.g., "My Mobile App" or "Headless Frontend").
- Click the Add New Application Password button.
- WordPress will generate and display a unique password. Copy this password immediately, as it will not be shown again.
- Store this password securely in your application's configuration or environment variables.
For more detailed instructions, refer to the WordPress REST API Application Passwords guide.
For OAuth 1.0a
To use OAuth 1.0a, you typically need to install and activate an OAuth 1.0a Server plugin for WordPress. Once installed, the process generally involves:
- Registering an application: Within the plugin's settings, you will register your application, which typically involves providing a name, description, and callback URL. This process will generate a Consumer Key and Consumer Secret for your application.
- Implementing the OAuth flow: Your application will then need to implement the standard OAuth 1.0a three-legged flow to obtain a Request Token, authorize it via the user, and exchange it for an Access Token.
Specific steps may vary based on the OAuth plugin you choose. Consult the documentation of your chosen OAuth 1.0a plugin for precise instructions on client registration and token acquisition.
Authenticated request example
This example demonstrates how to make an authenticated request using an Application Password and Basic Authentication to create a new post.
Using cURL with Application Passwords
Replace your_username with the WordPress username associated with the application password, your_application_password with the generated password, and your_wordpress_site.com with your site's domain.
curl -X POST \
https://your_wordpress_site.com/wp-json/wp/v2/posts \
-H "Content-Type: application/json" \
-H "Authorization: Basic $(echo -n 'your_username:your_application_password' | base64)" \
-d '{ "title": "My New Post", "content": "This is the content of my new post.", "status": "publish" }'
In this example, $(echo -n 'your_username:your_application_password' | base64) generates the Base64 encoded string of your username and application password, prefixed with Basic in the Authorization header, as required by RFC 7617 for Basic HTTP Authentication.
Security best practices
When working with WordPress REST API authentication, adhering to security best practices is essential to protect your site and user data.
- Use HTTPS: Always ensure your WordPress site and all API requests are served over HTTPS. This encrypts the communication channel, preventing sensitive information like application passwords or OAuth tokens from being intercepted in plain text. Many hosting providers offer free SSL certificates, or you can use services like Cloudflare to secure your site with SSL encryption.
- Principle of Least Privilege: Grant only the necessary permissions to the user account associated with an Application Password or OAuth access. For example, if an application only needs to read posts, do not grant it administrator privileges.
- Securely Store Credentials: Never hardcode application passwords or OAuth secrets directly into your application's source code. Use environment variables, secure configuration files, or secret management services (e.g., AWS Secrets Manager, Google Secret Manager) to store and retrieve credentials.
- Regularly Rotate Credentials: Periodically generate new Application Passwords and revoke old ones. For OAuth, ensure your application handles token expiration and refresh mechanisms gracefully.
- Monitor API Activity: Implement logging and monitoring for API access to detect unusual activity or potential misuse. WordPress plugins are available that can log REST API requests.
- Revoke Unused Passwords/Tokens: If an application is no longer in use, or if you suspect a compromise, immediately revoke its associated Application Password or OAuth access token.
- Validate and Sanitize Inputs: When accepting data via the REST API, always validate and sanitize all inputs to prevent common web vulnerabilities such as SQL injection or cross-site scripting (XSS). The WordPress REST API includes built-in sanitization, but additional validation on the client-side or server-side can add layers of defense.
- Implement Rate Limiting: Protect your API from brute-force attacks or excessive requests by implementing rate limiting. This can be done at the server level (e.g., Nginx, Apache), through a CDN like Cloudflare, or via WordPress security plugins.