C# how to define global constants like api keys and prevent from being uploaded to Github

Viewed 104

I'm working on a Visual Studio C# project (.net 4.7.2) where I need to store some private API keys as constants. The project gets uploaded to a public github repo, but I don't want the API keys to be shared.
What is the best practice for achieving this?

It seems if I create a Constants class file with a static class containing the constant values, then omit file via .gitignore, the project won't build for other users who download it because the file will be missing. If I upload the skeleton of the file first, then omit it via .gitignore, it won't be updated if I change or add new constants later.

I'm looking for best practices to achieve this.

1 Answers

a pattern that I see often and I personally use:

  • create a skeleton file like settings.json.sample and put placeholder values for your keys
  • copy that file to settings.json and put the real values
  • add settings.json to .gitignore
  • in the readme add instructions to rename settings.json.sample to settings.json and populate with actual values
Related