Azure Functions to generate unique identifiers

  • Sandro Pereira
  • May 11, 2023
  • 4 min read

There are so many scenarios I’ve faced over 18 years in integration that required me to generate a unique ID, and I don’t know how to start explaining them all. Unique identifiers are handy when an automation process needs to generate a unique reference “number” to identify an object or entity, like a Customer account, a document ID, and so on. Most of the time, we use GUIDs, which stand for a globally unique identifier, and it is usually a 128-bit text string that represents an identification (ID) that is unlikely ever to repeat or create a collision, to address these scenarios unless requirements don’t allow us to use a GUID.

This is a list of 4 Functions that will allow you to generate unique identifiers:

  • GUID Generator: This function has the same capabilities available on the Online GUID / UUID Generator: https://guidgenerator.com/online-guid-generator.aspx.
  • Tickets Short GUID Generator: Calculate the uniqueId using the provided year.
  • YouTube-like GUID Generator: Function to generate a short GUID like in YouTube (N7Et6c9nL9w).
  • Tiny ID Generator: Another way that I used in the past to generate a tiny identifier.

📝 One-Minute Brief

Generating unique identifiers is a common requirement in integration scenarios. This article presents four Azure Functions that generate different types of unique IDs, from standard GUIDs to short and compact identifiers, helping you choose the right approach based on your technical and business requirements.

GUID Generator Function

As mentioned above, this function has the same capabilities as the Online GUID / UUID Generator: https://guidgenerator.com/online-guid-generator.aspx. It accepts 3 optional query parameters:

  • useHyphen: accepts the values true or false to decide if you want to generate a guide separated with hyphens.
    • The default value is true.
  • useUppercase: accepts the values true or false to decide if you want to convert the GUID to upper case.
    • The default value is false.
  • useBraces: accepts the values true or false to decide if you want to generate a guide between braces.
    • The default value is false.
Generate Guid

Here is a small sample of the code behind this function:

        if (useHyphen)
        {
            Guid hyphenGuid = Guid.NewGuid();
            string finalGuid = "";

            if (useBraces)
                finalGuid = hyphenGuid.ToString("B");
            else finalGuid = hyphenGuid.ToString();

            if (useUppercase)
                return new OkObjectResult(finalGuid.ToUpper());
            return new OkObjectResult(finalGuid);
        }

Tickets Short GUID Generator Function

This function generates a unique identifier based on the year. This function accepts 1 optional query parameter:

  • year: year to take as a reference for calculating the unique identifier.
    • The default value is 1978.
Tickets Short Guid

Here is a small sample of the code behind this function:

var ticks = new DateTime(year, 1, 1).Ticks;
var ans = DateTime.Now.Ticks - ticks;
var uniqueId = ans.ToString("x");

YouTube-like GUID Generator Function

This function allows you to generate a short GUID like in YouTube (N7Et6c9nL9w).

YouTube-like GUID

This was made just for fun, but here is a small sample of the code behind this function:

tring base64Guid = Convert.ToBase64String(Guid.NewGuid().ToByteArray());

Tiny ID Generator Function

This function is another way that I used in the past, in BizTalk Server projects, to generate a tiny identifier. But looking now that I’m writing this blog post, it looks like a YouTube-like GUID.

Tiny ID Generator

Here is a small sample of the code behind this function:

Guid guid = Guid.NewGuid();
            string modifiedBase64 = Convert.ToBase64String(guid.ToByteArray())
                .Replace('+', '-').Replace('/', '_') // avoid invalid URL characters
                .Substring(0, 22);

Where can I download it?

You can download the complete Azure Functions source code here:

Once again, thank my team member, Luis Rigueira, for testing and helping me develop some of these functions with me!

Hope you find this helpful! So, if you liked the content or found it helpful and want to help me write more content, you can buy (or help buy) my son a Star Wars Lego! 

Author: Sandro Pereira

Sandro Pereira lives in Portugal and works as a consultant at DevScope. In the past years, he has been working on implementing Integration scenarios both on-premises and cloud for various clients, each with different scenarios from a technical point of view, size, and criticality, using Microsoft Azure, Microsoft BizTalk Server and different technologies like AS2, EDI, RosettaNet, SAP, TIBCO etc. He is a regular blogger, international speaker, and technical reviewer of several BizTalk books all focused on Integration. He is also the author of the book “BizTalk Mapping Patterns & Best Practices”. He has been awarded MVP since 2011 for his contributions to the integration community.

Leave a Reply

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

The Ultimate Cloud
Management Platform for Azure

Supercharge your Azure Cost Saving

Learn More
Turbo360 Widget

Back to Top