WordPress API Tokens: image displaying a key and WordPress logo with connections in the background

Create WordPress API Tokens with JWT Authentication: A Step-by-Step Guide

In today’s fast-paced digital world, automation is key to efficiency. For businesses using WordPress, connecting your website to automation platforms like Zapier, Make, or other applications can streamline workflows, save time, and boost productivity. To do this securely, you need a WordPress API token.

This guide will walk you through creating an API token on your WordPress site using the JWT Authentication for WP REST API plugin. This will allow you to securely connect your website to the vast world of automation platforms, empowering you to create powerful, automated workflows.

http://googleusercontent.com/image_generation_content/0

Why Use API Tokens for WordPress Automation?

By default, the WordPress REST API allows applications to interact with your site’s data. However, for many automation scenarios, you need a secure and reliable way to authenticate these connections. This is where the JWT Authentication for WP REST API plugin shines. It provides a secure method for authenticating with your WordPress site using JSON Web Tokens (JWT), ensuring that only authorized applications can access your data.

Once you have your API token, you can use it to:

  • Automate social media posting: Automatically share new blog posts to your social media channels.
  • Sync with CRMs: Add new users to your CRM or update existing contacts.
  • Integrate with email marketing services: Subscribe new users to your newsletter.
  • Connect to e-commerce platforms: Sync orders and customer data.
  • And much more! The possibilities for automation are virtually limitless.

Step-by-Step Guide to Creating Your API Token

Here’s how to get started with the JWT Authentication for WP REST API plugin to generate your token:

1. Install and Activate the Plugin

  • From your WordPress dashboard, navigate to Plugins > Add New.
  • Search for “JWT Authentication for WP REST API”.
  • Click Install Now and then Activate.

2. Configure the Plugin

The free version of this plugin requires some manual configuration. You’ll need to edit your wp-config.php and .htaccess files.

  • .htaccess Configuration: Most hosting providers disable the HTTP Authorization Header by default. To enable it, you’ll need to add the following code to your .htaccess file, which is located in the root directory of your WordPress installation:
RewriteEngine on 
RewriteCond %{HTTP:Authorization} ^(.*) 
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
  • wp-config.php Configuration:
    • Define the Secret Key: You need to add a unique and secret key to your wp-config.php file to sign the tokens. You can generate a secure key from the WordPress Salt Key Generator. Then, add the following line to your wp-config.php file, replacing 'your-top-secret-key' with your generated key: define('JWT_AUTH_SECRET_KEY', 'your-top-secret-key');
    • Enable CORS Support: To allow cross-origin requests, add the following line to your wp-config.php file: define('JWT_AUTH_CORS_ENABLE', true);

3. Generating a Token

Once the plugin is configured, you can generate a token by sending a POST request to the following endpoint:

https://YourDomainHere.com/wp-json/jwt-auth/v1/token

The body of the request should contain the username and password of a WordPress user with the appropriate permissions:

JSON

{
    "username": "your_username",
    "password": "your_password"
}

A successful request will return a JSON object containing your JWT token. This token is what you’ll use to authenticate with other services.


Using Your API Token with Automation Platforms

Now that you have your JWT token, you can use it to authenticate your requests to the WordPress REST API from your chosen automation platform.

When setting up your automation, you’ll typically need to include the token in the Authorization header of your API requests, using the “Bearer” scheme. For example:

Authorization: Bearer <your_jwt_token>

With this setup, you can now create automated workflows that interact with your WordPress site securely and efficiently.

References

For more information, you can refer to the following resources:

  1. JWT Authentication for WP REST API Plugin Page
  2. WordPress REST API Handbook
  3. Zapier WordPress Integration

Do you need help with your project?

Get a free quote.