Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save naveenvm93/2c395c735654cb1946d782d95b5d6202 to your computer and use it in GitHub Desktop.
Save naveenvm93/2c395c735654cb1946d782d95b5d6202 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<body>
%%[
IF RequestParameter("submitted") == "submitted" Then
]%%
<script runat="server">
Platform.Load("Core", "1");
try {
var email = Request.GetQueryStringParameter("EmailAddress");
var firstname = Request.GetQueryStringParameter("firstname");
var lastname = Request.GetQueryStringParameter("lastname");
//authenticate to get access token
var authEndpoint = 'https://xxxxxx.auth.marketingcloudapis.com/' //provide API endpoint
var payload = {
client_id: "Your Client ID", //pass Client ID
client_secret: "Your Client Secret", //pass Client Secret
grant_type: "client_credentials" ,
account_id : "Your MID" //pass MID
};
var url = authEndpoint + '/v2/token'
var contentType = 'application/json'
var accessTokenRequest = HTTP.Post(url, contentType, Stringify(payload));
if (accessTokenRequest.StatusCode == 200) {
var tokenResponse = Platform.Function.ParseJSON(accessTokenRequest.Response[0]);
var accessToken = tokenResponse.access_token //get access token
var rest_instance_url = tokenResponse.rest_instance_url
};
//make api call to Trigger an Transactional email
if (accessToken != null) {
//Start Logic for Incremental counter to generate the unique ID which is necessary for single transactional email sends
var Count = Platform.Function.Lookup('Test_Transactional_API_Counter','Count','UniqueId','Test_Transactional_API_Counter');
Count = Count + 1;
var updateCount = DataExtension.Init("Test_Transactional_API_Counter");
updateCount.Rows.Update({Count:Count}, ["UniqueId"], ["Test_Transactional_API_Counter"]);
//End Logic for Incremental counter to generate the unique ID which is necessary for single transactional email sends
var headerNames = ["Authorization"];
var headerValues = ["Bearer " + accessToken];
var jsonBody = {
"definitionKey": "Test_Transactional_Message_V2", //Email definition Key
"recipient":
{
"contactKey": email,
"to": email,
"attributes": {
"FirstName": firstname,
"LastName": lastname
}
}
};
var requestUrl = rest_instance_url + "/messaging/v1/email/messages/"+Count; //URL for Transactional API with unique Id
var fireEmail = HTTP.Post(requestUrl, contentType, Stringify(jsonBody), headerNames, headerValues);
if (fireEmail.StatusCode == 202) {
var finalResponse = Platform.Function.ParseJSON(fireEmail.Response[0]);
var requestId = finalResponse.requestId
var errorcode = finalResponse.errorcode
var responses = finalResponse.responses[0].messageKey
var message = finalResponse.message
var rows = Platform.Function.InsertDE("Test_Transactional_API_Logs",["Message_key","RequestId","Errorcode","EmailAddress","firstname","lastname"],[Count,requestId,errorcode,email,firstname,lastname]); //Insert the email sent response into a data extension
}
};
} catch (error) {
Write(Stringify(error));
Write("Error");
}
</script>
%%[ENDIF]%%
<h2>Sign Up Form</h2>
<form action="%%=RequestParameter('PAGEURL')=%%" method="POST">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="firstname" value=""><br><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lastname" value=""><br><br>
<label for="lname">Email:</label><br>
<input type="text" id="email" name="EmailAddress" value=""><br><br>
<input id="submitted" name="submitted" type="hidden" value="submitted">
<input type="submit" value="Submit">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment