Ops love APIs: Here is Why

By Jonathan Sundqvist, Engineer at Server Density.
Published on the 7th April, 2016.

Inspired . . . Delighted . . . Hooked.

We always run out of superlatives when we talk about APIs.

Wait a minute, you say, haven’t APIs been around for awhile? Can’t you pick any of the newer high-powered acronyms to get excited about?

Here’s the thing. At Server Density we are quite religious about our workflow. Yes, we appreciate technology (and acronyms) in as far as they help us reclaim time for things that matter. Old school things like serving our customers and growing our business.

Automation is a big part of this. One of the reasons we love APIs is because they allow us to do just that: automate. Do things programmatically instead of, you know, grind it through the UI. All we need to do is prepare the scripts and let them run on their own.

We are not alone in this. As our largest deployments will attest, the ability to add hundreds of servers worth of inventory on Server Density is not something you want to do by hand.

We are our biggest API user

It’s not an add-on, it’s not a feature or a separate effort. The Server Density API is a core constituent of our product.

What is the number one, nonstop activity Server Density performs 24×7?

Push agent data to our time series database. What’s interesting is that the agent can only speak to the backend by means of the public API. We are not talking about private endpoints here. Any user can do their own postbacks. We use the same API calls as everyone else.

What about our UI? How does our frontend talk to the backend? Same story. Through that same public API. I don’t want to belabour the point. By definition, we are dogfooding all the time. Any pain points, we feel them before anyone else does.

Endless possibilities

Want to collect temperature data from your greenhouse? Use the API. Want to script and automate thousands of servers? The tools are there. Want to craft your own application and access our time-series database to create your own charts in, say, Excel? The tools are there. Gather, present and tweak your data in some new way? The API’s got you covered. We give you full, uninhibited access to our entire time series database. There is no limit to the data you can store and monitor.

What follows is a few cool things we’ve learned over the years. Think of them as common sense tips on how to use APIs in general.

1. Documentation is your friend

If something is not documented, it doesn’t exist. A lot of heart and soul goes into our documentation. It’s in our best interest to remove all barriers from API adoption and we try to spell things out as much as we can. We define a function (for example, Pagination) and add language-specific code examples to demonstrate how it works.

Server Density API

When it comes to APIs, it pays to refer to the documentation before you invest any time figuring things on your own.

2. Start Simple

Make sure your API queries work in well-defined and controlled environments, before you incorporate them in actual code. You won’t be able to troubleshoot otherwise.

Victor, one of our iOS developers, tests his API calls in the simplest of environments: a Server Density account with a single dashboard that contains a single device and metric. That’s the only way you can truly compare what you get against what you should be getting.

OPS loves API

The following short sample was generated by Paw. It fetches the device_alerts endpoint and prints the result:

func sendAlertMetricsRequest() {
    /**
     Alert metrics
     GET https://api.serverdensity.io/alerts/device_alerts.json
     */

    // Add URL parameters
    let urlParams = [
        "token":self.token
    ]

    // Fetch Request
    Alamofire.request(.GET, "https://api.serverdensity.io/alerts/device_alerts.json", headers: headers, parameters: urlParams)
        .validate(statusCode: 200..<300)
        .responseJSON { response in
            if (response.result.error == nil) {
                debugPrint("HTTP Response Body: \(response.data)")
            }
            else {
                debugPrint("HTTP Request failed: \(response.result.error)")
            }
        }
}

Start with just one instance or entity. Work with a reduced set of devices and metrics. Then expand with more. Make sure you cover all individual cases your app may encounter.

OPS loves API 23. Use API Wrappers

Don’t start work on any new API without first checking to see if there are wrappers for it. If there are none available, you may want to invest some effort in building your own and make your life easier.

A good API wrapper is the ultimate text expander. It means you have fewer parameters and URLs to remember, and even fewer lines of code to type.

At this point, we offer public PHP and Python wrappers for the Server Density API. We are also working on a Swift wrapper (a result of our work with the Apple TV app).

Get started with the Server Density Python wrapper

The following command fetches the wrapper from pypi and installs it on your computer.

pip install sd-python-wrapper 

Let’s say you have a bunch of servers you need to add on Server Density. Here’s how you can do that with the Python wrapper:

from serverdensity.wrapper import Device
token = 'your token'

list_of_device_names = ['what', 'you', 'want', 'your' 'devices', 'to', 'be', 'named', 'in', 'server density']

for name in list_of_device_names:
    device = Device(token, name=name)

response = device.create()

The wrapper parses all required properties and checks their format is correct, helping you catch any errors early on. Here is an example:

from serverdensity.wrapper import Service

service = Service(token, name='my service name')

# the above will raise an AttributeError since there a couple
# of other properties that are required for successfully creating a service

Summary

We couldn’t be more excited about the potential of public APIs. Our API sits right at the very core of Server Density. It’s what offers access to our time series database and the terabytes worth of processing that happens behind the scenes.

Good APIs unlock a wealth of functionality and enable automations to help engineers reclaim time. We spend a significant time making the Server Density API as powerful and accessible as possible.

We also provide wrappers to minimise manual effort and make everyone’s life a tiny bit easier. Head over to our API wrapper repo, take our Python wrapper for a spin and send us your feedback.

Articles you care about. Delivered.

Help us speak your language. What is your primary tech stack?

Maybe another time