API keys isn't working

API keys isn't working

It’s the same API key, why it’s not working? 🤔

It happens to all developers and sometimes we stare at API keys and we wonder why it doesn’t work, it’s the same value from the credentials page or it looks like it.

So, how can we be sure that we are seeing everything? Before, you start thinking about a crafty script and tokenization plus counting the number of characters/tokens. Let’s keep things simple.

First, whitespaces are invisible, so unless you have a magic lenses you can distinguish between these values:

ABC
ABC 

In Unix based system, we can use the following command to substitute whitespace with $.

# Add an extra space after the value.
export KEY=ABC  
echo -n $KEY | tr '[:space:]', '$'

The output will be ABC$.

Feel free to choose a different replacement character that is different from the last one in your value, it’ll save your sanity.

After finding the invisible culprit, it’s time to fix the environment variable properly. So instead of jumping in the server console and removing the extra whitespace, I would fix the value in Kubernetes, CI, or any configuration that you are using for the deployment. And don’t forget to restart your instance after the changes, otherwise, you will have nightmares from whitespaces.