Skip to content

Instantly share code, notes, and snippets.

@eliotharper
Created September 15, 2020 23:39
Show Gist options
  • Save eliotharper/65f2cf34ea8da01f980314f07ce22f4c to your computer and use it in GitHub Desktop.
Save eliotharper/65f2cf34ea8da01f980314f07ce22f4c to your computer and use it in GitHub Desktop.
Example SSJS for SFMC that retrieves weather forecast from an external API
<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