Sending Mails from Foxx in the background via SendGrid

ArangoDB Foxx allows defining job queues that let you perform slow or expensive actions asynchronously.

These queues can be used to send emails, call external APIs or perform other actions that you do not want to perform directly or want to retry on failure. Let’s say you want to send out an email every time you check off an item in your Foxx todo app and you want to use an external transactional email service to do that. Go to

SendGrid and sign up for a free account there. You will need your credentials (your username and password) to configure the Foxx Job for SendGrid. First, install the job by going to the admin interface of ArangoDB and clicking on ‘Applications’ and then ‘Add Application’. Now click on the ‘Store’ button and search for the mailer-sendgrid app from Alan Plum. Click the ‘Install’ button next to it.

Mount it to /sendgrid and provide your username and password from SendGrid. After that press configure, and the app will be installed and configured. Now, let’s go to your app.

In your controller, first add a variable for a new queue with

queue = Foxx.queues.create('todo-mailer', 1)

This will create a new queue with the name todo-mailer and give you access to it via the queue variable.

Now we want to push a job onto our queue every time somebody adds an item to the todo list (the route that posts to /). Here is the according code (don’t forget to adjust the to field to be your email address):

js'/', function (req, res) { var todo = req.params('todo');; res.json(todo.forClient());
queue.push('mailer.sendgrid', { from: '', 
                                to: 'YOUR ADDRESS', 
                                subject: 'New Todo Added', 
                                html: 'New Todo added: ' + todo.get('title') }); 
}).bodyParam('todo', 'The Todo you want to create', Todo);

And that’s it! To try it out open your todo app from the Applications tab in the ArangoDB web interface and create a new todo item with the interactive documentation. You will receive an email with the title of the todo shortly after that. As we are using the queue, the user of your API doesn’t need to wait until the email is sent, but gets an immediate answer.

Read the Foxx Queues documentation to get familiar with handling queues in your Foxx apps.

Dr. Frank Celler

Frank Celler

Frank is both entrepreneur and backend developer, developing mostly memory databases for two decades. He is the CTO and co-founder of ArangoDB. Try to challenge Frank asking him questions on C, C++ and MRuby. Besides Frank organizes Cologne’s NoSQL group & is an active member of NoSQL community.

Leave a Comment

Get the latest tutorials, blog posts and news: