Skip to content

Commit

Permalink
Merge pull request #532 from roel-de-vries/add-environment-explanation
Browse files Browse the repository at this point in the history
Added information about setting up environment variables
  • Loading branch information
thinkingserious authored Oct 4, 2017
2 parents 88a8a90 + f4189b1 commit 00d1510
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
2 changes: 2 additions & 0 deletions ExampleCoreProject/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ private static void Main()

static async Task Execute()
{
// Retrieve the API key from the environment variables. See the project README for more info about setting this up.
var apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");

var client = new SendGridClient(apiKey);

// Send a Single Email using the Mail Helper
Expand Down
4 changes: 2 additions & 2 deletions ExampleNet45ASPNetProject/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

## Setup Environment Variables

Update the development Environment with your SENDGRID_API_KEY.
Update the "SendGridApiKey" appSettings variable within the web.config file.

## Steps

Expand All @@ -22,4 +22,4 @@ You may need to download the latest Nuget executable and run `nuget.exe restore`

## Happy coding

Big thanks to [paritosh baghel](https://github.com/paritoshmmmec) for contributing this example code!
Big thanks to [paritosh baghel](https://github.com/paritoshmmmec) for contributing this example code!
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace SendGrid.ASPSamples
public class SendGridEmailService
{
private readonly SendGridClient _client;
private string apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
private string apiKey = ConfigurationManager.AppSettings["SendGridApiKey"];
private static readonly string MessageId = "X-Message-Id";

public SendGridEmailService()
Expand Down Expand Up @@ -44,7 +44,7 @@ public EmailResponse Send(EmailContract contract)
return ProcessResponse(_client.SendEmailAsync(emailMessage).Result);
}

private EmailResponse ProcessResponse(Response response)
private EmailResponse ProcessResponse(Response response)
{
if (response.StatusCode.Equals(System.Net.HttpStatusCode.Accepted)
|| response.StatusCode.Equals(System.Net.HttpStatusCode.OK))
Expand Down
2 changes: 1 addition & 1 deletion ExampleNet45ASPNetProject/SendGrid.ASPSamples/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-SendGrid.ASPSamples-20170430084902.mdf;Initial Catalog=aspnet-SendGrid.ASPSamples-20170430084902;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="SendGridApiKey" value="SendGridApiKey" />
<add key="SendGridApiKey" value="YOUR_API_KEY" />
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
Expand Down
2 changes: 2 additions & 0 deletions ExampleNet45Project/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ private static void Main()

static async Task Execute()
{
// Retrieve the API key from the environment variables. See the project README for more info about setting this up.
var apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");

var client = new SendGridClient(apiKey);

// Send a Single Email using the Mail Helper
Expand Down
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ For sample implementations, see the [.NET Core Example](https://github.com/sendg

<a name="quick_start"></a>
# Quick Start
## Manage keys using Environment Variables
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.

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, and making them easy to manage.

This example will go through adding an environment variable in Windows (10).

If we look at a code example from the quick start we see that the second line of the `Execute()` method tries to retrieve an enviroment variable:
```csharp
var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_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 on "Enviroment 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 on "Ok", and again in the variable overview and lastly close the system properties window.

You maybe have to restart your IDE you were working in to make use of the new variable.

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

## Hello Email

Expand Down Expand Up @@ -228,4 +251,4 @@ sendgrid-csharp is guided and supported by the SendGrid [Developer Experience Te

sendgrid-csharp is maintained and funded by SendGrid, Inc. The names and logos for sendgrid-csharp are trademarks of SendGrid, Inc.

![SendGrid Logo](https://uiux.s3.amazonaws.com/2016-logos/email-logo%402x.png)
![SendGrid Logo](https://uiux.s3.amazonaws.com/2016-logos/email-logo%402x.png)

0 comments on commit 00d1510

Please sign in to comment.