Abbreviations For Code

One of SparkPost’s taglines is “robust cloud email API,” so it probably doesn’t surprise you that our service has an awesome API that makes it really easy to send email. Our API also enables certain types of account configuration, which you usually don’t need to call in an automated way. For instance, the simplest way to set up a relay webhook is by writing a cURL command and sending it off to the API.

Another good example is adding an inbound domain. Using cURL, here’s how you’d write out this relatively tame beast.

Fire Breathing Dragon GIF by Game of Thrones - Find & Share on GIPHY

curl -XPOST \ \
  -H "Authorization: MY_SUPER_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "domain": "" }'

Pretty straightforward, right? But as Larry Wall, the creator of Perl, once quipped, laziness is a virtue in programmers. That’s why we build tools and automate tasks. Consider the example above. Most of the data in that request will be repeated in every SparkPost API call. Plus, doing it this way means you have to store your API key somewhere easily accessible. So how do we make this easier? Why, a command line interface!

Opening The Black Box

We recently refactored some of our client libraries to be very thin wrappers on top of our API. This makes maintaining them much easier, while still providing a common interface between the different languages.

With this mindset we wanted to build something that would be both configurable, mirror the SparkPost API, and require relatively little maintenance. To achieve this, we generated the SparkPost CLI from our node library, which has resource wrapper objects to improve the user experience.

Next, we set up a configuration file and merged it with the configuration that we generated from the library. Lastly, we handed the combined configuration off to yargs, a powerful library for writing CLIs in node.

Take The SparkPost CLI For A Spin

Grab the SparkPost CLI from NPM and configure it with your API key.

npm install sparkpost-cli -g
sparkpost config --key=MY_SUPER_SECRET_KEY

Now to add an inbound domain, run the following command, which is substantially more tame, and significantly less boilerplate-y than our earlier, verbose cURL example.

sparkpost inbound-domains create

Happy How I Met Your Mother GIF - Find & Share on GIPHY

Releasing It To You


Our hope is that this tool will make our API easier to use for the more mundane, ad hoc tasks like creating a webhook or grabbing data from the Message Events API. Right now the SparkPost CLI has support for 7 of our API endpoints, and there are more coming soon.

Give it a try and let us know what you think! We live for community feedback. If you’re really into it, dig around the code and maybe even make a pull request. Then hop on over to Slack or Twitter and let us know what you think.

Software Engineer