REST APIs are a popular way to expose the functionality of a web application to other systems through the internet. They enable other systems to interact with your application using HTTP requests and receive responses in a standard format. In this guide, we will create a REST API using the Taffy framework and the JWT-CFML library for authentication in the Lucee CFML engine.
In this example, we will use Docker and Docker Compose to run the application. The CFML engine is running inside a CommandBox container, but you can also use CommandBox directly or any other Lucee installation. Note that while this example is specifically tailored for Lucee, it may also work on other engines with some adjustments.
You can find a working example of this guide on my GitHub page:
Setting Up Taffy
To set up Taffy, we first need to download the latest release from GitHub. If you want to create an API for an existing application, create a subfolder in which Taffy will reside. For the purposes of this tutorial, we will name this subfolder "api". Clone or unpack Taffy in this subfolder.
We only need the following files/folders from Taffy for this example: - /bonus/* - /core/* - /dashboard/* (optional for development) - /examples/* (optional for development)
You can delete everything else. For testing purposes, you can use the Taffy dashboard or a tool like Postman.
Next, create an empty "index.cfm" file. This file is necessary for Taffy to work and should be left blank. All requests are funneled through it and handled by the framework's internal functionality.
After that, create the "application.cfc" file for Taffy. For now, we will add the basic Taffy configuration and a array containing some books.
To create resources that can be served by your API, create a folder called "resources" and a file called "getBooks.cfc" inside it.
⚠️
To access the API, you can use the endpoint "/api/index.cfm/getAllBooks". If you want to remove "index.cfm" from the endpoint, you will need to add a rewrite rule to your web server. You can find more information on this in the taffy documentation.
Add JWT Authentication
Now, we can begin implementing JWT-based authentication for our API. While Taffy offers other options like basic authentication, we will focus on using JSON Web Tokens for this guide.
To get started, we need to download the JWT-CFML library from the latest release on GitHub. Once the download is complete, unpack the contents into a subfolder where your API is located. I recommend to create a folder specifically for the JWT-CFML library, such as "jwt". This will help keep your API organized and make it easier to reference the library in the future.
Now, we can initialize the JWT library and create the "onTaffyRequest()" function to handle the decoding and validation of the JSON Web Tokens. I have also included a hardlink to the authentication route for your reference.
Now, we will create the authentication resource where users of the API can request tokens for their own use in identifying themselves to the system.
Now, you should be able to request a token through the authentication route by providing the correct API key. This token can then be used to authenticate yourself and access the "getAllBooks" resource.
In conclusion, we have successfully implemented REST API functionality using the Taffy framework and JWT-CFML library for authentication in the Lucee CFML engine. We set up resources that can be accessed through specific endpoints, and added an extra layer of security with JWT-based authentication. Don't forget to check out the working example on my GitHub page for more details and to see everything in action. Thanks for following along!
If you need any assistance or have suggestions for improvement, please leave a comment or contact me via email.