1

I'm trying to shorten a URL and put it into an SMS in Marketing Cloud Journey Builder. I've been on their website, registered, got a Key and managed to short the url in JSON using Postman. Now...how do I make a AMPSCRIPT code for my SMS? I've seen this article with the response from Eliot Harper but it's for bitly and not short.io. Is there something similar? I'm pretty new to AMPscript and don't really know too well how to proceed. This is what I've been able to write:

%%[
var @ContactKey, @shortenUrl, @shortioUrl, @link
set @ContactKey= AttributeValue('ContactKey')
set @link = URLEncode(CloudPagesURL(6944,'ContactKey',@ContactKey))
set @shortioUrl = 'https://api.short.io/links?authorization=XXXXXXX&hostname=XXXXXXX&linkType=secure'
set @shortenUrl = HTTPGet(Concat(@shortioUrl,@link,"&format=txt"))
]%%

Hello @name,
follow this link %%=v(@shortenUrl)=%% !

Thanks everyone.

4
  • 3
    Hi and welcome to SFSE. This community is here to help, but it’s not intended as a free coding service. Please give it a try first and come back in case you are stuck or you have problems with your code. The example you found with bitly is a great start and you should be able to build something very similar off of it for short.io
    – zuzannamj
    Commented Dec 27, 2022 at 17:56
  • Oh sorry didn't mean that. I've corrected my question. Hope it's ok now.
    – Alex1986
    Commented Dec 28, 2022 at 7:54
  • Thanks for sharing the Ampscript. But what's wrong with it? We need some more context, and error messages (if any), in order to help you. Right now, you have just replaced the URL in Elliot's solution, and that's not too much effort from your side. Commented Dec 28, 2022 at 11:28
  • The problem is that I don't know if this string is right: set shortioUrl = 'api.short.io/…' I've found also this: {set shortioUrl = 'api.short.cm/links/…' But when I try to send the SMS through the Journey I don't recieve anything. That's what I'm trying to achieve here. I don't know if the parameters that I'm passing are correct. Can't find anything in the guide.
    – Alex1986
    Commented Dec 28, 2022 at 14:54

1 Answer 1

2

Instead of starting with AMPscript, I'd suggest reading through the Short.io API documentation and doing a proof-of-concept using Postman.

I went through the short.io registration process and did a test run myself. Pretty slick.

Here's generic export of what I used from Postman. Import this collection into your Postman: https://gist.github.com/wvpv/173192f2e21e0138e04844606fb7437d

This is the result I got from Postman:

Request:

POST /links HTTP/1.1
Host: api.short.io
authorization: MYAPITOKENHERE
Content-Type: application/json
{
    "originalURL": "https://sprignaturemoves.com/",
    "domain": "link.sprggs.com"
}

Response:

{
    "originalURL": "https://sprignaturemoves.com/",
    "DomainId": 11111,
    "archived": false,
    "lcpath": "11111",
    "source": "api",
    "cloaking": false,
    "createdAt": "2022-12-28T16:29:46.592Z",
    "updatedAt": "2022-12-28T16:29:46.592Z",
    "OwnerId": 11111,
    "tags": [],
    "path": "Ea1e5a",
    "idString": "MYAPITOKENHERE",
    "shortURL": "https://link.sprggs.com/Ea1e5a",
    "secureShortURL": "https://link.sprggs.com/Ea1e5a",
    "duplicate": false
}

Now, the AMPscript. You can find a basic HTTPPost2 example here.

The tricky part is parsing the JSON response with AMPscript. It's best to leverage SSJS for that, IMO.

%%[

set @endpoint = "https://api.short.io/links"
set @contentType = "application/json"
set @URLToShorten = "https://sprignaturemoves.com"
set @payload = Concat('{"originalURL": "',@URLToShorten,'","domain": "link.sprggs.com"}')
set @APIKey = "MYAPITOKENHERE"
set @headerKey1 = "authorization"
set @headerVal1 = @APIKey

SET @request = HTTPPost2(@endpoint,@contentType,@payload,true,@responseJSONStr,@responseRows,@headerKey1,@headerVal1)

set @responseRowCount = rowcount(@responseRows)
output(concat("<br>payload:",@payload))
output(concat("<br>request:",@request))
output(concat("<br>responseJSONStr:",@responseJSONStr))
output(concat("<br>responseRowCount:",@responseRowCount))

]%%
<script runat="server" language="JavaScript">
   
   /* SSJS to parse JSON payload (don't @ me about GTL) */
   var responseJSONStr = Platform.Variable.GetValue("@responseJSONStr")
   var responseJSONObj = Platform.Function.ParseJSON(responseJSONStr);
   var shortURL = Platform.Variable.SetValue("@shortURL", responseJSONObj.shortURL);

</script>
<br>shortURL: %%=v(@shortURL)=%%

Output:

payload:{"originalURL": "https://sprignaturemoves.com","domain": "link.sprggs.com"}
request:200
responseJSONStr:{"lcpath":"11111","createdAt":"2022-12-28T16:55:59.973Z","source":"api","DomainId":111111,"archived":false,"OwnerId":11111,"updatedAt":"2022-12-28T16:55:59.973Z","originalURL":"https://sprignaturemoves.com","tags":[],"cloaking":false,"path":"11111","idString":"MYAPITOKENHERE","shortURL":"https://link.sprggs.com/GoJc5F","secureShortURL":"https://link.sprggs.com/GoJc5F","duplicate":true}
responseRowCount:8
shortURL: https://link.sprggs.com/GoJc5F

You can retrieve the shortURL value from the response using a simple JS dot reference. You should be able to retrieve any of the other values from the Postman response that you did first.

NOTE: Probably should add some error handling as a fallback for if the POST fails or if you're unable to parse the response.

2
  • Thanks for the great response Adam! Too bad it doesn't seem to be able to use ssjs in SMS.
    – Alex1986
    Commented Dec 29, 2022 at 14:02
  • You'll have to put your code in a Code Resource block in Content Builder and include it in your message using %%=ContentBlockByKey("YOUREXTERNALKEY")=%% Commented Dec 29, 2022 at 14:57

Not the answer you're looking for? Browse other questions tagged .