Skip to content

Commit 00d1510

Browse files
Merge pull request #532 from roel-de-vries/add-environment-explanation
Added information about setting up environment variables
2 parents 88a8a90 + f4189b1 commit 00d1510

File tree

6 files changed

+33
-6
lines changed

6 files changed

+33
-6
lines changed

ExampleCoreProject/Example.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ private static void Main()
1616

1717
static async Task Execute()
1818
{
19+
// Retrieve the API key from the environment variables. See the project README for more info about setting this up.
1920
var apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
21+
2022
var client = new SendGridClient(apiKey);
2123

2224
// Send a Single Email using the Mail Helper

ExampleNet45ASPNetProject/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
## Setup Environment Variables
1111

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

1414
## Steps
1515

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

2323
## Happy coding
2424

25-
Big thanks to [paritosh baghel](https://github.com/paritoshmmmec) for contributing this example code!
25+
Big thanks to [paritosh baghel](https://github.com/paritoshmmmec) for contributing this example code!

ExampleNet45ASPNetProject/SendGrid.ASPSamples/Service/SendGridEmailService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace SendGrid.ASPSamples
1212
public class SendGridEmailService
1313
{
1414
private readonly SendGridClient _client;
15-
private string apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
15+
private string apiKey = ConfigurationManager.AppSettings["SendGridApiKey"];
1616
private static readonly string MessageId = "X-Message-Id";
1717

1818
public SendGridEmailService()
@@ -44,7 +44,7 @@ public EmailResponse Send(EmailContract contract)
4444
return ProcessResponse(_client.SendEmailAsync(emailMessage).Result);
4545
}
4646

47-
private EmailResponse ProcessResponse(Response response)
47+
private EmailResponse ProcessResponse(Response response)
4848
{
4949
if (response.StatusCode.Equals(System.Net.HttpStatusCode.Accepted)
5050
|| response.StatusCode.Equals(System.Net.HttpStatusCode.OK))

ExampleNet45ASPNetProject/SendGrid.ASPSamples/Web.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<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" />
1313
</connectionStrings>
1414
<appSettings>
15-
<add key="SendGridApiKey" value="SendGridApiKey" />
15+
<add key="SendGridApiKey" value="YOUR_API_KEY" />
1616
<add key="webpages:Version" value="3.0.0.0" />
1717
<add key="webpages:Enabled" value="false" />
1818
<add key="ClientValidationEnabled" value="true" />

ExampleNet45Project/Example.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ private static void Main()
1616

1717
static async Task Execute()
1818
{
19+
// Retrieve the API key from the environment variables. See the project README for more info about setting this up.
1920
var apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
21+
2022
var client = new SendGridClient(apiKey);
2123

2224
// Send a Single Email using the Mail Helper

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,29 @@ For sample implementations, see the [.NET Core Example](https://github.com/sendg
7171

7272
<a name="quick_start"></a>
7373
# Quick Start
74+
## Manage keys using Environment Variables
75+
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.
76+
77+
Environment variables are usually part of the OS and are available to programs within that same OS.
78+
It gives the option to bypass hardcoding credentials, and making them easy to manage.
79+
80+
This example will go through adding an environment variable in Windows (10).
81+
82+
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:
83+
```csharp
84+
var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
85+
```
86+
`NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY` isn't a variable of your OS environment yet, so let's add it.
87+
88+
Press `win + R` (or search for "run"), fill in "SystemPropertiesAdvanced", press enter and then click on "Enviroment variables".
89+
You'll get two lists of variables, but we'll focus on the User Variables for this example.
90+
91+
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).
92+
For value insert your SendGrid API key. Click on "Ok", and again in the variable overview and lastly close the system properties window.
93+
94+
You maybe have to restart your IDE you were working in to make use of the new variable.
95+
96+
Now, if all went well you can access the just added variable in your C# SendGrid projects! Ready for some examples?
7497

7598
## Hello Email
7699

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

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

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

0 commit comments

Comments
 (0)