Use case: generate and implement UUIDs

As a developer or admin, there are certain situations when you need to generate a unique identifier for an object. Of course, you could create a random string to do this, but the better and more popular way is to assign a universally unique identifier (UUID) to the object. UUIDs are also sometimes referred to as GUIDs, or globally unique identifiers.

Generating and assigning UUIDs can be done at the origin, but this approach doesn’t work if the objects you need to identify are served from an edge server's cache. In these instances, you can generate and assign UUIDs at the Edge — Akamai’s intelligent edge platform — and thereby improve performance and your website visitors' experience.

UUIDv4 is designed to generate IDs from random numbers, while UUIDv5 IDs are derived from a namespace. Adhere to the specifications defined in RFC 4122, and you can use convenient and standardized tools and methods to create random-number UUIDs and namespace-based UUIDs.

The basic format for a UUID is five segments beginning with 8 hex characters, followed by three 4-character strings, then 12 characters at the end. These segments are separated by -. Here’s what the five segments look like in a sample UUID:
123a4567-b89c-12d3-e456-789012340000

You can generate UUIDs with the Hex Random function in the Property Manager Set Variable behavior.

UUIDv4

Follow these steps to generate UUIDv4s using the Hex Random generator function in Property Manager’s Set Variable behavior menu.

  1. Create temporary variables for the five segments of the UUID format.

  2. Use the Set Variable behavior to create individual segment values for the first, second, and fifth segment.
    The first 12 hex characters (Segments 1 and 2) and the last 12 hex characters (Segment 5) are very simple: all of those characters should be random data.


  3. Use this formula to generate the third segment of the UUID.

    • Create a random number from 0 to 4095, and add 16384
    • Convert to hexadecimal
  4. Use this formula to generate the fourth segment of the UUID.

    • Create a random number from 0 to 16383, and add 32768
    • Convert to hexadecimal
  5. Combine the segments.
    Concatenate all five segments, separate them with dashes, and store the resulting string in a local variable.

PAPI code snippet

To implement UUIDv4s with Property Manager API, insert this code block:

{

"name": "Create UUID",

"children": [],

"behaviors": [

{

"name": "setVariable",

"options": {

"variableName": "PMUSER_UUID_PART1",

"valueSource": "GENERATE",

"transform": "NONE",

"generator": "HEXRAND",

"numberOfBytes": 4

}

},

{

"name": "setVariable",

"options": {

"variableName": "PMUSER_UUID_PART2",

"valueSource": "GENERATE",

"transform": "NONE",

"generator": "HEXRAND",

"numberOfBytes": 2

}

},

{

"name": "setVariable",

"options": {

"variableName": "PMUSER_UUID_PART3",

"valueSource": "GENERATE",

"transform": "ADD",

"generator": "RAND",

"minRandomNumber": "0",

"maxRandomNumber": "4095",

"operandOne": "16384"

}

},

{

"name": "setVariable",

"options": {

"variableName": "PMUSER_UUID_PART3",

"valueSource": "EXPRESSION",

"transform": "DECIMAL_TO_HEX",

"variableValue": "{{user.PMUSER_UUID_PART3}}"

}

},

{

"name": "setVariable",

"options": {

"variableName": "PMUSER_UUID_PART4",

"valueSource": "GENERATE",

"transform": "ADD",

"generator": "RAND",

"minRandomNumber": "0",

"maxRandomNumber": "16383",

"operandOne": "32768"

}

},

{

"name": "setVariable",

"options": {

"variableName": "PMUSER_UUID_PART4",

"valueSource": "EXPRESSION",

"transform": "DECIMAL_TO_HEX",

"variableValue": "{{user.PMUSER_UUID_PART4}}"

}

},

{

"name": "setVariable",

"options": {

"variableName": "PMUSER_UUID_PART5",

"valueSource": "GENERATE",

"transform": "NONE",

"generator": "HEXRAND",

"numberOfBytes": 6

}

},

{

"name": "setVariable",

"options": {

"variableName": "PMUSER_UUID",

"valueSource": "EXPRESSION",

"transform": "NONE",

"variableValue": "{{user.PMUSER_UUID_PART1}}-{{user.PMUSER_UUID_PART2}}-{{user.PMUSER_UUID_PART3}}-{{user.PMUSER_UUID_PART4}}-{{user.PMUSER_UUID_PART5}}"

}

}

],

"criteria": [],

"criteriaMustSatisfy": "all",

"comments": "This code creates a random UUID and stores it on a predefined user variable called PMUSER_UUID. Use the value on this variable for any logic needed.  For example, inject it in a cookie or a header. Note: First 4 bits of segment 3 and first 2 bits of segment 4 are fixed  per RFC 4122"

UUIDv5

You can also generate UUIDv5s, which are basically a hashing of the name that the UUID is based on, in this case the URL.

  1. Set some variables, including the fixed initial value for the URL namespace ID.
    Take the namespace ID as defined in the RFC. For example, 6ba7b811-9dad-11d1-80b4-00c04fd430c8.

  2. Append the URL to the namespace ID.
    For example, you'd append https://www.example.com/test/ to the above namespace ID to get this string: 6ba7b811-9dad-11d1-80b4-00c04fd430c8https://www.example.com/test/

  3. Hash the string using SHA-1.

  4. Put the character at position 17 into its own variable.

  5. Extract a value based on the current value of the character.

  6. Select characters from the SHA-1 hash, add the UUID version number 5 and the newly calculated character as shown to create the UUID.

PAPI code snippet

To implement UUIDv4s with Property Manager API, insert this code block:

{

                    "name": "Create UUID v5",

                    "children": [],

                    "behaviors": [
                    {
                        "name": "setVariable",
                        "options": {
                            "variableName": "PMUSER_UUID",
                            "valueSource": "EXPRESSION",
                            "transform": "SHA_1",
                            "variableValue": "{{user.PMUSER_UUID_NS_URL}}{{builtin.AK_URL}}"
                        }
                    },
                    {
                        "name": "setVariable",
                        "options": {
                            "variableName": "PMUSER_UUID_FIXED",
                            "valueSource": "EXPRESSION",
                            "transform": "SUBSTRING",
                            "variableValue": "{{user.PMUSER_UUID}}",
                            "startIndex": "16",
                            "endIndex": "17"
                        }
                    },
                    {
                        "name": "setVariable",
                        "options": {
                            "variableName": "PMUSER_UUID_FIXED",
                            "valueSource": "EXPRESSION",
                            "transform": "EXTRACT_PARAM",
                            "variableValue": "0=8/1=9/2=A/3=B/4=8/5=9/6=A/7=B/8=8/9=9/A=A/B=B/C=8/D=9/E=A/F=B",
                            "paramName": "{{user.PMUSER_UUID_FIXED}}",
                            "separator": "/",
                            "caseSensitive": true
                        }
                    },
                    {
                        "name": "setVariable",
                        "options": {
                            "variableName": "PMUSER_UUID",
                            "valueSource": "EXPRESSION",
                            "transform": "SUBSTITUTE",
                            "variableValue": "{{user.PMUSER_UUID}}",
                            "regex": "(.{8})(.{4}).(.{3}).(.{3})(.{12}).*",
                            "replacement": "$1-$2-5$3-{{user.PMUSER_UUID_FIXED}}$4-$5",
                            "caseSensitive": true,
                            "globalSubstitution": false
                        }
                    },
                    {
                        "name": "setVariable",
                        "options": {
                            "variableName": "PMUSER_UUID",
                            "valueSource": "EXPRESSION",
                            "transform": "LOWER",
                            "variableValue": "{{user.PMUSER_UUID}}"
                        }
                    }
                    "criteria": [],

 "criteriaMustSatisfy": "all",

 "comments": "This code creates a name based UUID (v5) and stores it on a predefined user variable called PMUSER_UUID. Use the value on this variable for any logic needed like for example inject it in a cookie or a header. Note: First 4 bits of part 3 and first 2 bits of part 4 are fixed as per RFC 4122"

}

Did this page help you?