API Conversations and Why They’re Important

Nick Schmidt
4 min readJun 6, 2023

API Interactions are designed to be easy

Believe it or not, the IT infrastructure industry is trying to make things easier by building API access out.

Programmatic interfaces are a new mental model competing for brain-space with GUI and CLI implementations; we need to play to its strengths:

  • Parsing output from a computer automatically
  • Easy batch entry
  • Ensuring that a thing configured matches the thing requested
  • (Usually) easy pass/fail responses based on HTTP response codes

API Conversation Structure

RESTful, SOAP, and NetCONF interfaces all interleave the concept of a conversation with the Hypertext Transfer Protocol (HTTP) standards:

  • Identify Yourself
  • Ask for things
  • Get a Response

NB: Most API implementers (vendors) will implement API standards very loosely! NB: RESTful interfaces will be used for the examples in this post, as it’s quickly becoming the most common.

Authentication

Authentication is where most new users get stuck. It’s complicated, but usually an API provider will also leverage an SDK to simplify the authentication process when you “graduate” to a programming language.

This only covers what a client has to do to perform API work — implementing an API (and authorization with it) is considerably more complex.

Basic Authentication

The title says it all, this authentication schema just uses Base64 encoding (ASCII-formatted binary) to place your username and password in an HTTP header:

If this approach makes you feel a little uncomfortable, it should. This is not a secure way to execute commands; it puts your credentials at risk. There are a few ways to mitigate the security risks:

  • Ensure that any endpoint you interact with uses strong cryptography and has a valid certificate
  • Postman and cURL do half of that by default
  • If it’s an API you manage, issue it a client-recognizable certificate and tune cryptography upwards
  • Try using a “read-only” account unless a change is required
  • Use Basic Authentication as a method to establish a session token

Most new API sessions will at least start using Basic authentication, so these guidelines will apply unless client certificate authentication is used.

Here are some examples of client authentication use:

cURL Python 3

Token Authentication

Most API Providers will require you to use safer authentication for continued requests. This is a good thing — but it does add of work. Usually, Basic or Certificate authentication is used to establish a timeboxed token for future authentication.

These tokens have several forms:

  • Bearer Token: Simple. They’ll use the Authorization header per the HTTP standard:
  • JSON Web Token: Standardized and much more robust. Base64 encodes a JSON payload. It supports signing of all requests and provides context in all future requests, which makes things easier to parse on the server side
  • I’ll just call this third category weird stuff. Vendors loosely follow standards, so they’ll typically have headers that are specific to their platform. The mechanics remain the same, but the header name won’t be Authorization

Here are some examples:

cURL Python 3

Verbs

In API Terminology, clients (and servers) should identify what type of operation they intend to execute with a matching HTTP Method:

  • GET: Read only actions, typically without a submitted payload.
  • POST: Frequently (mis)used. Can be read only, can be a non-idemopotent change, or it could be an idempotent change. By standard, POST methods create an object in a tree or change the state machine - but nearly every vendor defaults to this method.
  • PUT: Meant to be a "safe" method that either updates an existing record, or creates a new object. Usually, if an API provider uses this verb, they're using it properly.
  • PATCH: PUT is idempotent, which is safe from an API perspective, but not from a production service perspective. It will wipe and recreate a service - the PATCH verb indicates a messier but production safe action.
  • DELETE: Idempotent, but it does delete a resource, so I'd be careful considering it production-safe.

Why?

Infrastructure operators benefit from programmatic interface usage, but the pattern differs.

Researching secure, well-managed authentication methods leaves a lot of room for improvement with the commodity resources available today, and a security engineer with even some basic API knowledge can quickly and easily secure resources with these rules. It’s easy to imagine how firewalling an API service can quickly become secure:

Essentially, any load balancer can become a highly granular firewall for an API.

APIs are easy to parse due to their dictionary formats. Prior to their inception, network engineers had to write screen scrapers like Scrapli and “guess” what value in a given table has what meaning. YAML and JSON formats allow language-native association between values not normally present in a tab-separated table like a routing table.

The most important advantage to API automation, though, is for change safety. No matter what discipline you follow, invasive IT work is completed at night and with a strict schedule — one that does not promote thoroughness. Leverage APIs to check if your system is healthy — only you as the engineer know how to do that — and make change windows less stressful.

Some Tips

  • jq is a command-line utility that parses JSON - and lets you navigate or prune it to relevant data. Think of it like grep for JSON.
  • If you do complex or scaled work with Python, check out tqdm to build progress bars for your work.

Originally published at https://blog.engyak.co on June 6, 2023.

--

--

Nick Schmidt

I am a network engineer based out of Alaska, pursuing various methods of achieving SRE/NRE