Need help with actionhero-tutorial?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

143 Stars 48 Forks 331 Commits 0 Opened issues


An example actionhero project demonstrating many common features

Services available


Need anything else?

Contributors list

Actionhero Tutorial

Actionhero Logo
  • Created: June 22, 2013
  • Updated: Jan 18, 2021

Node.js CI

This guide will walk you through the creation of the application in this repository, and in the process, you will learn some of the basics of Actionhero.

You will become comfortable with the following topics:

A simple Blogging API & site:

Adding a chat room

Next Steps

Actionhero Resources


  • You are expected to have a basic familiarity with node.js and the command line
  • You do not need to clone this repository to follow this guide. The code in this repository represents the final state of a project created with these instructions. The code in this project should serve as a reference.
  • You should also be able to run this project by:
    • git clone
    • cd actionhero-tutorial
    • npm install
    • npm run dev
      npm start
    • There are a few extra steps needed to persists data to Redis.
  • This project uses
    as a database. You will need to have it installed on your computer.
  • Remember that Actionhero is an API server, so we will be focusing on creating an API for blogging and chatting, and applying that to a website rather than creating a beautiful website itself.

Getting Started with a new actionhero Project

files discussed in this section:

relevant documentation section:

Actionhero is a node.js package. Be sure you have node.js (version >= 10.0.0) installed. Node comes with npm, the node package manager.

includes a command
we can use to run actionhero simply.

This guide was written on OSX It should be appropriate for any version of OSX > 10.6. It should also work on most Linux distributions (Ubuntu, CentOs, Fedora, etc). The concepts presented here should also be appropriate for windows users, but many of the "Getting Started" commands will not work as described here. If you are looking for help on getting started on Windows, please join the Actionhero Slack Channel to get help.

Create a new directory for this project and enter it (in the terminal):

  • mkdir ~/actionhero-tutorial
  • cd ~/actionhero-tutorial

note: From this point forward, it is assumed that all commands listed are run from within the


Use the Actionhero generator to build your project

  • npx actionhero generate

Install any project dependencies

  • npm install

Try to boot the Actionhero server in development mode

  • npm run dev

You should see the default Actionhero welcome page at

(visit in your browser)

The port

is defined in
, along with all other settings for Actionhero. Actionhero has two types of HTTP routes: static files and api routes. static files are served from
and the API is served from
. These routes are configurable from files within
. Actionhero also picks one of these to be the default root route. This is defined by
. We want our default route to serve our
file from
, so we can leave this setting as "file".

Since you are running in development mode (

npm run dev
), Actionhero will notice you made a change to a config file and reboot the server automatically for you. Now, visit
and you should see the welcome page. You will note that the setting we just changed was under the
section. This is because this setting is only relevant to HTTP clients, and not the others (websocket, etc). We will talk about these more later.

We should also enable all the servers which ship with Actionhero (websocket). Enable them by setting

in their config files.

Actionhero uses the variable

to determine which modification file to load from
to load to modify the default values in
. This is how you can set different variables per environment. We will use this for testing later.

Creating Modules

files discussed in this section:

relevant documentation section:

Modules are common libraries you create to help you manipulate data. These are normal Typescript functions/modules/classes. Here, we will build 2 that help us manage users and posts. Typescript will then make all the methods and classes available for us in the rest of our application once we require the module

I'll define some common blog functions we'll use later in actions in


Our blogging methods are:

Const Blog = {
  // posts
  postAdd: async function(...) {},
  postView: async function(...) {},
  postsList: async function(...) {},
  postEdit: async function(...) {},
  postDelete: async function(...) {},
  // comments
  commentAdd: async function(...) {},
  commentsView: async function(...) {},
  commentDelete: async function(...) {}

Const Users = { // users add: async function (...) {}, list: async function (...) {}, authenticate: async function (...) {}, del: async function (...) {}, };

A few things to note:

  • posts are hashes with the content and some additional metadata
  • comments are also hashes, a key for each comment
  • we always make asynchronous functions, using
  • at this layer, we don't worry about authentication or validations. That will be handled in our Actions later on.
  • we are making use of
    to talk to redis. To make that simpler, we've made a
    method to reference the redis client object for us.

Creating Actions

files discussed in this section:

relevant documentation section:

Now that we have our helpers for getting and setting blog posts, how can we allow users to use them? Actions!

The files in

can define a one or more Actions each, so let's create one for comments and one for posts.
  • npx actionhero generate-action --name=users
  • npx actionhero generate-action --name=blog

Extending the

Actions can be extended by extending the

class. We want to denote on each of our actions whether or not we require an authenticated user (to make a new post) or they are public (anyone can read the blog) - We'll use
action.authenticated = true
to denote that the action requires authentication. However, that property is not a normal property of the class
. We can make a new class,
which we can then extend:
// from classes/authenticatedAction

import { Action } from "actionhero";

export abstract class AuthenticatedAction extends Action { authenticated: boolean; }


files discussed in this section:

relevant documentation section:

Now that we've defined our Actions, we want to expose them via the HTTP server. Actionhero does this via a

file. Routes allow different HTTP verbs to preform a different action on the same URL. We'll use a
file to transform our API into restful resources for users, comments, and posts. You can derive input variables from the structure of URLs with routing as well.
  • I can add a user with
    curl -X POST -d "userName=evan" -d "password=password" "http://localhost:8080/api/user"
  • I can log in with
    curl -X POST -d "userName=evan" -d "password=password" "http://localhost:8080/api/authenticate"
  • I can create a post with
    curl -X POST -d "password=password" -d "title=first-post" -d "content=My%20first%20post.%20%20Yay." http://localhost:8080/api/post/evan
    (the user name is derived from the route)
  • I can view that new post with
    curl -X GET "http://localhost:8080/api/post/evan/first-post"
  • I can get a list of my posts with
    curl -X GET http://localhost:8080/api/posts/evan
  • Another user could add a comment with:
    curl -X POST -d "comment=cool%20post" -d "commenterName=Someone_Else" "http://localhost:8080/api/comment/evan/first-post"
  • And we can see the comments with:
    curl -X GET "http://localhost:8080/api/comments/evan/first-post"

Once you define your routes, you can visit the Swagger page (

) to see the automatic documentation of your HTTP API Actions!

Middleware and Initializers

files discussed in this section:

relevant documentation section:

In the steps above, we created a

method, but didn't use it anywhere. We also denoted that we wanted some actions to be authenticated with
authenticated: true
, but didn't create any logic to do so... now we will with an
Action Middleware

First, Let's create a new initializer for this:

  • npx actionhero generate-initializer --name=middleware

Middleware can run before and after every action (global), or just those actions that opt into them. In our case, we'll make a global middleware which applies to all actions and check if the action being run should be authenticated or not. The

lifecycle hook we are using will run before the Action, but will have access to the params sent by the user - like
so we can check if they are correct.

Middleware are added to the api by adding them via



files discussed in this section:

When you generate a new Actionhero project, we will generate tests for you to be run with the Jest framework. Actionhero exposes a number of utilities to make it easy to boot up a server with configuration overrides to make testing easier. We'll also use the

package to make HTTP requests simpler in our tests.
npm install --save-dev request
(which will add the package to your
in the

We can now run the test with the

command. In our
we already have
npm test
configured to run the test suite how we would like it:
"test": "jest"
. Jest will automatically set
for us, to tell Actionhero we are running this command in the 'test' environment this will signal Actionhero load any config changes from the
file's TEST configurations, if they are different. In our case, we set up redis and the servers a little differently when testing.

A successful test run looks like this:

We also use the npm lifecycle command

to run a linter,
. This helps our code to conform to a consistent style and will check for errors like variable scope or missing variables.

Consuming the API via the Web

files discussed in this section:

relevant documentation section:

Actionhero is primarily an API server, but it can still serve static files for you. In

, the
directive is where your web site's "root" is. You can also use actions to manipulate file content with the
method. Actionhero is also a great choice to power your front-end applications (angular, react, ember, etc).

Provided in

is a simple page that demonstrates how to call an action to show some status about your sever, using the
action (generated with a new project).


files discussed in this section:

relevant documentation section:

demonstrates how to use Actionhero's websockets. The
is a first-class protocol in Actionhero and has all the capabilities of
clients - and more! Web sockets are persistent connections which also enables Actionhero's chat room features. We will make use of them here.

Note how we make use of the event libraries of

and build our events around it. This library is accessed by including
in your html page.:
client = new ActionheroWebsocketClient();

// register for events client.on("connected", function () { console.log("connected!"); }); client.on("disconnected", function () { console.log("disconnected :("); }); client.on("error", function (error) { console.log("error", error.stack); }); client.on("reconnect", function () { console.log("reconnect"); }); client.on("reconnecting", function () { console.log("reconnecting"); }); client.on("welcome", function (message) { appendMessage(message); }); client.on("say", function (message) { appendMessage(message); });

client.connect(function (error, details) { if (error) { console.error(error); } else { // run an action client.action("createChatRoom", { name: "defaultRoom" }, function (data) { // join a chat room client.roomAdd("defaultRoom"); }); } });


files discussed in this section:

relevant documentation section:

Actionhero comes with a robust task system for delayed/recurring tasks. For our example, we are going to create a task which will log some stats to the command line every 30 seconds. You can do much more with Actionhero's task system, including distributed tasks, recurring tasks, and more.

npx actionhero generate-task --name=stats --queue=default --frequency=30000
  • note how we set the
    to run every 30 seconds
  • to enable our server to run tasks, we need to configure 'workers' to run. You can enable
    (set them both to 1).
  • to re-schedule a job like ours, you will also need to enable the scheduler process in

Next Steps / TODO

  • Use cookie-based authentication rather than requiring the password and userName to be sent with each request
  • Migrate to another database
  • Implement a UI for the API
  • Tests should be more inclusive, and test failure cases
  • Pagination for all the

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.