Skip to content

Commit

Permalink
Merge pull request #701 from sidkcr/master
Browse files Browse the repository at this point in the history
Improved environment variable setup instructions
  • Loading branch information
thinkingserious authored Jun 25, 2018
2 parents 29042f2 + 9590394 commit 52ce0ae
Showing 1 changed file with 16 additions and 33 deletions.
49 changes: 16 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
<a name="announcements"></a>
# Announcements

## August 2017

Subscribe to email [notifications](https://dx.sendgrid.com/newsletter/csharp) for releases and breaking changes.


Expand Down Expand Up @@ -59,47 +57,32 @@ Grab your API Key from the [SendGrid UI](https://app.sendgrid.com/settings/api_k

## Setup Environment Variables to Manage Your API Key

Do not hard code your [SendGrid API Key](https://app.sendgrid.com/settings/api_keys) into your code. Instead, use something like an [environment variable](http://superuser.com/questions/949560/how-do-i-set-system-environment-variables-in-windows-10) or [Web.config](https://docs.microsoft.com/en-us/aspnet/identity/overview/features-api/best-practices-for-deploying-passwords-and-other-sensitive-data-to-aspnet-and-azure).

The examples found below in this quick start and in the example projects all make use of storing their API keys as an environment variable.
Manage your [SendGrid API Keys](https://app.sendgrid.com/settings/api_keys) by storing them in Environment Variables or in [Web.config](https://docs.microsoft.com/en-us/aspnet/identity/overview/features-api/best-practices-for-deploying-passwords-and-other-sensitive-data-to-aspnet-and-azure). It is a good practice to keep your data and configuration settings separate. This way to you can change your SendGrid API key without changing your code. Also, we strongly advise against storing sensitive data directly in your code.

Environment variables are usually part of the OS and are available to programs within that same OS.
It gives the option to bypass hardcoding credentials, making them easy to manage.
Setup Environment Variables using the UI:
1. Press Win+R and run SystemPropertiesAdvanced
2. Click on Environment Variables
3. Click New in user variables section
4. Type SENDGRID_API_KEY in the name. (Make sure this name matches the name of key in your code)
5. Type actual API Key in the value
6. Restart the IDE and you're done!

The following example will go through adding an environment variable in Windows 10.
Setup Environment Variables using CMD:
1. Run CMD as administrator
2. setx SENDGRID_API_KEY "YOUR_API_KEY"

Looking at the code example from the [Quick Start](#quick_start), we see that the second line of the `Execute()` method tries to retrieve an environment variable like so:
Here are few example to get and set API Keys programatically:

Get Environment Variable
```csharp
var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
```

`NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY` isn't a variable of your OS environment yet, so let's add it.

Press `win + R` (or search for "run"), fill in "SystemPropertiesAdvanced", press `enter` and then click "Environment variables".

You'll get two lists of variables, but we'll focus on the User Variables for this example.

Click the "New" button beneath the User Variables list. Give it the name `NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY` (or rename the variable in both the environment and code to a more generic name).

For value, insert your SendGrid API key. Click "Ok", and once again in the Variable Overview and lastly, close the system properties window.

For other Windows version, you can set an environment variable like this:

Set Environment Variable
```csharp
var setKey = Environment.SetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY", value);
var setKey = Environment.SetEnvironmentVariable("SENDGRID_API_KEY", "YOUR_API_KEY");
```

For example,

```csharp
var setKey = Environment.SetEnvironmentVariable("SENDGRID_API_KEY", YOUR_API_KEY);
```

You may need to restart your IDE to make use of the new variable.

Now, if all went well, you can access the newly added variable in your C# SendGrid projects! Ready for some examples?

## Install Package

To use SendGrid in your C# project, you can either <a href="https://github.com/sendgrid/sendgrid-csharp.git">download the SendGrid C# .NET libraries directly from our Github repository</a> or if you have the NuGet package manager installed, you can grab them automatically:
Expand Down

0 comments on commit 52ce0ae

Please sign in to comment.