how to assign meesageid variable in UUID format?

Not applicable

how can I assign meesageid variable in UUID format?

or can I generate a UUID in APIGEE?

0 4 2,936
4 REPLIES 4

adas
Participant V

By default apigee generates a variable called messageid. Its a unique id generated per request so if you are looking for something unique, you can simply use that.

If you want to write your own UUID generation logic, you can use a javascript policy. Here's a sample:

 function guid() {
  function s4() {
    return Math.floor((1 + Math.random()) * 0x10000)
      .toString(16)
      .substring(1);
  }
  return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
    s4() + '-' + s4() + s4() + s4();
}

var uuid = guid();
context.setVariable('response.header.x-apigee-uuid', uuid); 
//this returns the uuid as a response header called x-apigee-uuid

This code doesn't generate a valid UUID. You can find the format in https://en.wikipedia.org/wiki/Universally_unique_identifier (you can also read the RFC 4122 if you have more time). Unfortunately I can' find anywhere a peace of code that generate it. The unique solution that I have got is to use a java callout. What do you think ?

Hi @Roberto Molli

There is a java callout already created by my colleague Dino for this purpose -- see How to generate cryptographically secure random number in Edge.

You can generate UUID using Apigee's message template.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM.set-uuid">
    <DisplayName>AM.set-uuid</DisplayName>
    <Properties/>
    <AssignVariable>
        <Name>x-uuid</Name>
        <Template>{createUuid()}</Template>
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>