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.