When it comes to global e-commerce, one of the most fundamental problems is serving content localized to where your customers are coming from. Localizing not just your website copy but also your prices, available currencies, and shipping options is a fundamental method in helping to drive sales, as if a customer sees something in their local currency, then they are more likely to make a purchase, safe in the knowledge they don’t have any hidden charges such as currency conversion fees.
We had previously built a couple of e-commerce sites for our client using third-party e-commerce platforms, but found they were severely lacking when it came to this particular area, where setting up the ability to view different currencies was a difficult task, and to pay in them, nigh-on impossible at the time.
As we moved forward with developing our own bespoke lightweight e-commerce platform and headless CMS for the client, we looked into various ways on how to make handling localization and serving that localized content easier. While localizing the website content seemed relatively simple using templated files that simply pick up the correct text from a set of JSON files (one for each locale), identifying the user’s origin for things like the pricing seemed, at first, a lot more difficult.
While we looked at premium geolocation services, they were quickly ruled out by their high cost in comparison to the client’s budget and requirements. What we needed was something cheap, lightweight and could be used by any site running on our platform. After conducting some research, we discovered that the answer lay in the realm of Content Delivery Networks (CDNs), with most of them being able, in some form, to give you the country where the user’s request was coming from.
Now, knowing that this was built into CloudFront, we could exploit the fact that API Gateway uses the technology to write a public API endpoint that simply returned the “CloudFront-Viewer-Country” header, which gives you the country where the request to your endpoint originated from.
Considering the rest of our back-end technology stack was already based around Serverless APIs running on AWS, this solution fell into place quite quickly and was very easy to develop.
First, you set up a new Serverless API using either Serverless itself or an applicable NPM package. We use serverless-webpack for some of its optimization features which are useful for our larger APIs, but are not necessary in this instance.
Your Serverless yml files should look something like this:
Now all you need to do is create the handler, which just returns the CloudFront-Viewer-Country header in the response body.
Deploy this to your AWS account and you have a quick and easy way of getting the country from where the request originated.
In your front-end web application, you can then call the API to get the country of whoever is viewing the website and tailor the website content, pricing and shipping options to your customer’s specific location.
This kind of solution is great for clients who are working with AWS already, have smaller budgets, or are working on tight timescales, as it is quick to implement and cheap to run in comparison to third-party services.