A healthcheck endpoint is one of best things you can add to your API

A healthcheck endpoint is one of best things you can add to your API
Photo by Online Marketing / Unsplash

Whilst scrolling on Twitter earlier (yes, I still refuse to call it X), I stumbled across a Tweet by Gary Clarke:

This got me thinking about all of the APIs I've built and aside from one or two, I naturally end up building a healthcheck endpoint, just to verify the thing actually works.

The endpoint itself doesn't need to do anything fancy. Although the example in Gary's screenshot returns some JSON, I actually tend to just return a 200 OK response. This way, I can see if the API is up and make sure that I can use my API token to successfully authenticate with it.

Here's the endpoint for a SaaS I'm currently working on:

public function __invoke(Request $request): JsonResponse {
  return response()->json(['status' => 'ok']);
}

Example Healthcheck Endpoint

I can then create this in something like Postman as a request, send it to my API and ensure it works OK.