Let's take a look at an example of how we integrate with third party services from our Give & Get site (template).
We integrate with a (fictitious) delivery service to schedule deliveries for giveaways that visitors have requested.
The delivery service API works as follows:
origin
: The address of the giveaway giver.destination
: The address of the giveaway receiver.callbackURL
: URL of our site's HTTP function that the delivery service will call when the item is delivered.success
: Whether a delivery was successfully created.trackingURL
: A URL to a page for tracking the delivery.Note
To retrieve an API key to use the (fictitious) delivery service, go to our Blink Shipper site. If you're working on your own version of the Give & Get site, add the key to the Secrets Manager using the name deliveryKey.
Let's take a look at the code that creates a delivery for a requested giveaway. The code is in the deliveries.jsw backend web module.
We begin by importing the getSecret()
, fetch()
, and createCallbackURL()
functions.
The createCallbackURL()
function is imported from some backend code we've written. We'll see the definition of this function in a later lesson.
Next, in the exported createDelivery()
function, we create a callback URL using the createCallbackURL()
function.
After that, we get our key for the delivery API from the Secrets Manager and we store the URL of the API endpoint.
Next, we build the request body.
Then, we build the request options.
We define the request method to be POST. In the headers we set the right content type and send the authorization with the key we retrieved from the Secrets Manager. We also add the body we built above.
All that's left to do at this point is to make the API call and return the result.