Serverless with Azure Functions


The term serverless has been gaining traction in recent times. This is not entirely surprising as serverless brings several benefits to users. The term serverless is somewhat misleading: although the name suggests the absence of servers, it is not entirely the case.

“Just like wireless internet has wires somewhere, serverless architectures still have servers somewhere” ~

In this article, you’ll get introduced to the serverless architecture and learn how to build a simple API with Azure Functions.

What Exactly is Serverless?

The concept is quite simple. As a developer, you delegate the work of managing infrastructure(server) to a serverless vendor(Microsoft, Amazon, etc.) and focus only your code. The vendor manages the servers for your code, including automatically scaling to ensure your application handles load effectively.

“Serverless = “someone else is responsible for these servers going down” ~ @iamdevloper

Beyond server management being shifted to the vendor, serverless has attractive financial benefits. With several big names providing serverless platforms, prices are quite low and attractive. You only pay for the time and resources needed to run your code. This means, when your code is not running(idle), you don’t incur any cost. Moreover, a third-party handling your code infrastructure evidently reduces the money spent on acquiring infrastructure and the manpower to operate it.

“So here’s the hard facts – I’m dipping into my pocket every week to the tune of… $7.40 for you guys to do 54M searches against a repository of half a billion passwords 🙂” ~ Troy Hunt

Serverless In Action

In this section, we’ll build our little API with Azure Functions. Of course, there are several serverless alternatives you can check out: AWS Lambda, Google Cloud Functions, Alibaba Function Compute, Apache OpenWhisk, and Kubless.

Azure Functions is Microsoft’s serverless solution. It is quite simple to use; hence, the choice for our API. Our trivial API does nothing more than return a randomly generated number in a request body. Nonetheless, it gives us a gist of serverless and how to create serverless applications with Azure Functions.

You’ll need an Azure Account to follow along.

1. From the Azure Portal, select Create a resource > Computer > Function App

Fill in the details, then create. After a short moment, your Function App should be created.

2. Select the newly created app and add a function by clicking the “+” symbol. Choose the In-portal option next. Then, continue. Remember to choose JavaScript as the runtime stack as our example uses JavaScript.

3. Next, select Webhook + API since our function will be triggered via HTTP request.

4. An index.js is created with some boilerplate code. Replace it with this:

module.exports = async function (context, req) {

   if (req.query.user || (req.body && req.body.user)) {
       context.res = {
           status: 200,
           body: Math.random().toString()[3]
       context.log(`success: ${req.query.user || req.body.user} just asked for a random number`)
   else {
       context.res = {
           status: 400,
           body: "Sorry, I don't know you, let alone what to do"


Our function generates a single random number every time an HTTP request with a request body of user. You can test the function in Azure, under Test by specifying the body parameter.

Also note that although this is JavaScript, we don’t have a console object, instead a context object.

Take it for a Test
So far our API works but is not available to the world. To make it available, select Integrate on the right pane and change the Authorization Level from Function to Anonymous and save the changes. This makes the API available to the world. Anyone can make an HTTP request to our API and get a random number.

To obtain the API URL, go back to index.js and click Get function URL. We’ll use Postman to test the API.

After a few Postman tests, our API works fine. An interesting thing you’ll note using the API is that when you make a request after some time(say, 20 mins), it takes time to get a response. This is because the function goes into a sleep state and has to be restarted to process requests.

Congratulations. You just built a serverless API. Now it’s time to tell people about your world-changing API. Go on and add subscription and start making quick bucks 😆😆😆😆 .

Closing Thoughts

Serverless has great benefits, but it is not magic. Likewise, serverless is not a solution to all use cases. In this article, we have seen how to build a simple serverless API; the process is similar to other serverless platforms. Happy going serverless.

Bruno is a junior at Ashesi University College studying Computer Science. He is interested in leveraging the power of technology to increase productivity. As a big fan of open source technology, he is currently exploring the possibility of using the Bitcoin Blockchain to fight corruption in government. Bruno is a regular contributor at Fixate IO.


Click on a tab to select how you'd like to leave your comment

Leave a Comment

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

Skip to toolbar