When building an application it can be very useful to set up multiple environments. 

Why?

For example, you often want a staging environment to test new code before it goes to production, as well as a development environment on your laptop to run tests

Today we’ll explore some tips and tricks for doing so with Marketcloud.

Let's say that we want to build a shopping app and test out the checkout. You want to be sure that everything goes smoothly before pushing online a payment system. 

You want to get the correct user input from the interface and to send it in the proper way to Marketcloud. 

This requires you to create a lot of test data, such as test customers, test addresses, test products, test orders...

Creating those data inside our production environment is possible, however we don't really want to pollute our backoffice with fake data

What to do so? Follow our best practice!

We always suggest to create a separate application for testing and development, that's free to create one more app in Marketcloud ;)


Create a separate app for development

We can create a new application from our dashboard clicking the "new application" button on the top right corner.


Once this is done, we will have one more pairs of API key available from [DEV] Name app.
Note

We usually call them [DEV] Name app but you can use the notation you prefer.

Now in our code we can use different pairs of public and secret keys, separating staging and production environment.

To load the correct pair of Marketcloud keys we can use environment variables, creating a new variable for the public key and another for the secret key.

Let's see how to do it.


Configure different environments

Let's open our dashboard and click on the development app's settings button to open the development settings, then click on the settings tab. 

Here we can copy our development public key and secret key. 

These keys should be the one we use in our development machine, so store them in your computer's environment.

Note

The correct way to set environment variables changes with the OS. Look for the most correct way to create environment variables for your Operative System.

To create an environment variable in a linux system (Ubuntu in my case) you can run the following instructions in your terminal:


echo "export MARKETCLOUD_PUBLIC_KEY=my-public-key" >> ~/.profile
echo "export MARKETCLOUD_SECRET_KEY=my-secret-key" >> ~/.profile
source ~/.profile

This should be done in every separate environment you want to have, in our case we have a production environment and a development environment, so we will create these keys in our personal computer and in our live server.

At this point variables should be available in your application code, for example in NodeJS:

/*
NodeJS code, assuming we already 
created environment variables
*/
var marketcloudClient = new Marketcloud.Client({
   public_key : process.env.MARKETCLOUD_PUBLIC_KEY,
   secret_key : process.env.MARKETCLOUD_SECRET_KEY
})

At this point,  if we configured everything correctly, our local machine should make API requests against our development app, while our server should make API requests against our live app.

Pretty neat eh?


Conclusions

With these small tips we achieved a very sane separation of production and development/testing environment which allows us to do every kind of tests without interfering with the production application.

And do you know what is great about it?

This is good practice not only for Marketcloud keys but for every configuration string and credential you need in your code that should be secured and change with the environment.

At Marketcloud, we follow the 12factor app manifesto, a collection of best practices that we strongly recommend to follow.


Have suggestion? Drop us a line at info@marketcloud.it!