Practice making client- and server-side API calls
Your next task is to create events that correspond to the individual tours you offer.
Clone and set up the Napkin function
Navigate to the Napkin exercises here: https://www.napkin.io/n/a26cc45c0b89492a
Add this Napkin to your workspace by clicking Add to your functions.
Set up your workspace by adding your Klaviyo private API key as an environment variable, which can be found under the Other tab, in the same way you did for the first set of exercises.
If you need assistance, check out Napkin's documentation for environment variables.
Complete the exercises
In this set of exercises, you will track customer browsing activity using a client-side API call and their purchase activity using a server-side API call.
Instructions for this set of exercises are included in the Napkin function. There are two parts to complete:
- Track when customers view tours
- Record when customers book tours
The following documentation will be helpful as you work through these exercises:
Why server-side?
Think back to considerations for client-side vs. server-side API calls. Why would you want to send the Booked tour event server-side rather than client-side?
Check your work
If both events went through successfully, you should see both events on your test profile:
Stuck? Check out these resources
If you receive anything other than a 202 Accepted response, check out our guide to rate limits and error handling for guidelines on how to resolve any errors within your API call. For instance, a 401 Unauthorized error indicates that the authentication is incorrect, and you can double check our documentation to ensure that the authorization matches the required format.
Another common error is 400 Bad Request, which means that the request is incorrectly formatted. This usually stems from an error in the request body. Cross check your request with our API documentation to ensure that the request body is in the correct format.
You may also wish to make the API call in a tool like Postman to distinguish errors in the API call from errors in the Napkin code.
There are also a few scenarios where you may see a 202 Accepted status, but the events are still not appearing on the test profile. If this is the case, please make sure you are looking at the correct profile in the correct Klaviyo account (we recommend using email address to identify profiles).
If you are still stuck, have any questions, or want to share your work, please join and post in our Developer Group on Klaviyo Community.
Solution
Solutions can be found in this Napkin function: https://www.napkin.io/n/dab3245d915a4ee6
Why server-side?
Client-side API calls are sent publicly, and the data is publicly browsable. Therefore, sensitive data, like billing postal code, should never be sent in a client-side API call.
In general, client-side API calls should be used for events that happen while a customer is browsing, while server-side API calls are best for anything that happens after a customer starts the purchase process, such as starting a checkout.