Command-lining SendGrid for Developer Productivity


Posted on

TerminalI’m pretty bad at letting things break my flow. Twitter, Hacker News, the actual news, buying tech I don’t need; all things that are likely to happen if I leave my editor and head into the browser. Those tabs are just too damn easy to click on. Inspired by other developers and my fellow developer evangelists, I’ve been looking for ways to maximise my productivity when I’m coding.

So, I decided to start building an application that would take the elements of SendGrid administration I was doing, remove the browser element, and bring it back to the terminal. The end goal here is to render me, hopefully, distraction free. In this post I’ll show you how to quickly make an app that asks a user for their login details and returns a list of the current SendGrid inbound parse settings. This is a powerful, fun API for hacking that I use often.

Command the ‘grid

The code in the next section is a piece of a much larger project. CmdGrid is a simple command line application for interacting with the the SendGrid Parse API settings (and soon other elements of the SendGrid beast!).

I wrote the app using NodeJS and a library for working with command line input called Commander. Although processing command line arguments in NodeJS is relatively trivial, Commander gave it a cleaner feel that I found helpful on my first time out writing an app like this.

CmdGrid is available via NPM, so if you want to install it and give it a try it’s as simple as:

$ npm install cmdgrid

If it’s your first time using Node, you’ll want to grab the installer, which conveniently includes npm.

CmdGrid is super beta-might-be-broke-0.0.1 level right now, but it does what it needs to do. I’ll tidy it up in the coming weeks as I add more functionality. If you’re really interested, you can dig into the code on GitHub.

Once installed, you can access a bunch of features like adding and removing endpoints. Since we’ll be re-creating this piece, try this command:

$ cmdgrid -p list

Then CmdGrid will produce a table of inbound parse settings.

Writing Your Own Command Line App

Here’s how easy it can be to interact with SendGrid via the command line. Start by creating a new file

$ touch app.js

Then open it up in your editor of choice. Next we need to tell the app that it needs to run with NodeJS wherever it is run from and declare a couple of dependencies:

 

Commander will handle the command line interactions and the excellent Request library will make the HTTP requests we need to make super simple.

Next, let’s prompt for some input.

 

If you run the file at this point by typing

$ node app.js

you’ll be prompted for your user ID and api key… and that’s it. Not much fun right now.

Next we need to make a request, using those credentials, to the inbound parse API.

 

Above, we’ve prompted the user for their SendGrid credentials and passed them as a GET request to the Parse API settings endpoint. After we parse the JSON that gets returned we can loop through the responses and print them back out to the screen.

Wrapping it up

There we have it! In less than 30 lines of code we have a quick way of checking our Parse API settings from the command line. Go forth and experiment.

If you want to have full create/update/delete interaction then give CmdGrid a try and feel free to feedback on it in the comments or via GitHub.


Martyn Davies is a Developer Evangelist at SendGrid and a creative developer based in London. He has worked in technology for over 14 years with a background in both the music industry and technology. A serial hackathon organiser, mentor and startup advisor, you’ll find him presenting, demoing, hacking and chatting at hack days, conferences and meetups in the UK & Europe on a regular basis.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>