The Daily Adventures of a SteemPeak Developer #7 - Switch API Endpoint According to the User Country

Some days ago @robertyan asked in our Discord channel if it is possible to allow Chinese users to use SteemPeak. Before this change the default API endpoint used for new users was always and this endpoint is blocked in China.
As you may already know SteemPeak support other endpoints (at the moment just, but the user must be logged in in order to be able to change that setting. And CN users were not able to login in the first place without a VPN.

After some talks @r00sj3 opened a new issue on our repository to check if we can do something to improve the experience and allow login for those accounts.

Switch API Endpoint According to the User Country


Read other posts in the series HERE

At first I thought that this ticket would have required a large amount of work and research to find a good solution. But before moving it to a later release I did some research and it turned out that Cloudflare (our CDN) already did most of the work :)


For every request sent to our server the Cf-Ipcountry HTTP Header is added and I can easily check that on our server. If the header is found on a request the server just set a Cookie that the browser can read and use to configure the endpoint accordingly.

On the javascript app all I added was this fragment of code:

if (Cookie.get('COUNTRY') === 'CN') {
    steem.api.setOptions({ url: '' });

With this small change all the request to load contents are routed to the new endpoint. Unfortunately I cannot do much to help with the operation broadcast as that part don't happens on SteemPeak. Luckily users that login with Steem Keychain or the new SteemConnect v3 browser extensions will be able to manually set the endpoint in the browser extension itself.

Not too much code to share in this post (this is always a good thing in my opinion) but hopefully some of you will find the information in this post useful ;)

As always feedback and suggestion are much appreciated, both in the comments or on our Discord ;)


3 columns
2 columns
1 column