Authenticating with JWT


Installation

Install the Plugin

Install and activate the JWT Authentication for WP REST API plugin.

Define the Secret Key

Add the following constant to your wp-config.php file:

define('JWT_AUTH_SECRET_KEY', 'your-top-secret-key');

Generating an Authentication Token

To obtain a JWT token, send a POST request to:

POST http://your-site.com/wp-json/jwt-auth/v1/token

Required Parameters:

ParameterDescription
usernameThe WordPress username of the user you want to authenticate.
passwordThe password of the user.

Example Request (cURL):

curl -X POST http://your-site.com/wp-json/jwt-auth/v1/token \
     -H "Content-Type: application/json" \
     -d '{"username": "your-username", "password": "your-password"}'

Successful Response:

If the credentials are correct, the API will return a JSON response like this:

{
  "token": "your-jwt-token",
  "user_email": "[email protected]",
  "user_nicename": "username",
  "user_display_name": "User Name"
}

Using the JWT Token for Authentication

Once you have received the token, include it in the Authorization header for all future API requests.

Example Usage in PHP:

$token = 'your-jwt-token';
 
$headers = array(
 
    'Authorization' => 'Bearer ' . $token
 
);

Example Request (cURL):

curl -X GET http://your-site.com/wp-json/wp/v2/posts 
 
     -H "Authorization: Bearer your-jwt-token"

With JWT authentication enabled, your API requests are now secure. Use the token in the Authorization header for every authenticated API request.


← Previous
Next →