Created
September 15, 2020 23:39
-
-
Save eliotharper/65f2cf34ea8da01f980314f07ce22f4c to your computer and use it in GitHub Desktop.
Example SSJS for SFMC that retrieves weather forecast from an external API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script runat=server> | |
var apiKey = 'INSERT_WEATHERSTACK_API_KEY_HERE'; | |
var city = Platform.Request.GetQueryStringParameter('city'); | |
if (city) { | |
var url = 'http://api.weatherstack.com/forecast?access_key=' + apiKey + '&query=' + city; | |
var weather = Platform.Function.HTTPGet(url); | |
var obj = Platform.Function.ParseJSON(weather); | |
//Platform.Response.Write(obj.current.temperature); | |
Platform.Variable.SetValue('@city', city); | |
Platform.Variable.SetValue('@temp', obj.current.temperature); | |
Platform.Variable.SetValue('@feels', obj.current.feelslike); | |
} | |
</script> | |
%%[ if not Empty(@city) then ]%% | |
The current temperature in %%=v(@city)=%% is %%=v(@temp)=%%C and it feels like %%=v(@feels)=%%C. | |
%%[ endif ]%% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment