Skip to content

Commit 4c5ded1

Browse files
committed
Use API key environment variable by default
1 parent 187096f commit 4c5ded1

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

README.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ This guide provides several key sections:
1616
- [Gradle users](#gradle-users)
1717
- [Maven users](#maven-users)
1818
- [Others](#others)
19+
- [Set your API key](#set-your-api-key)
20+
- [Create your code](#create-your-code)
1921
- [API Reference](#api-reference)
2022
- [Creating an account](#creating-an-account)
2123
- [Test email addresses with Mailosaur](#test-email-addresses-with-mailosaur)
@@ -72,6 +74,24 @@ You'll need to manually install the following JARs:
7274
- [Google HTTP Client (Gson)](https://github.com/googleapis/google-http-java-client) from https://repo1.maven.org/maven2/com/google/http-client/google-http-client-gson/.
7375
- [Google Gson](https://github.com/google/gson) from https://repo1.maven.org/maven2/com/google/code/gson/gson/.
7476

77+
### Set your API key
78+
79+
Get your API key from the Mailosaur Dashboard and set it as an environment variable:
80+
81+
```sh
82+
export MAILOSAUR_API_KEY='your-api-key-here'
83+
```
84+
85+
### Create your code
86+
87+
Then import the library and create a client:
88+
89+
```java
90+
import com.mailosaur.MailosaurClient;
91+
92+
MailosaurClient mailosaur = new MailosaurClient();
93+
```
94+
7595
### API Reference
7696

7797
This library is powered by the Mailosaur [email & SMS testing API](https://mailosaur.com/docs/api/). You can easily check out the API itself by looking at our [API reference documentation](https://mailosaur.com/docs/api/) or via our Postman or Insomnia collections:
@@ -121,7 +141,7 @@ import java.io.IOException;
121141

122142
public class AppTest {
123143
@Test public void testExample() throws IOException, MailosaurException {
124-
MailosaurClient mailosaur = new MailosaurClient("API_KEY");
144+
MailosaurClient mailosaur = new MailosaurClient();
125145

126146
// See https://mailosaur.com/app/project/api
127147
String serverId = "abc123";
@@ -142,7 +162,7 @@ public class AppTest {
142162

143163
### What is this code doing?
144164

145-
1. Sets up an instance of `MailosaurClient` with your API key.
165+
1. Sets up an instance of `MailosaurClient` using the `MAILOSAUR_API_KEY` environment variable.
146166
2. Waits for an email to arrive at the server with ID `abc123`.
147167
3. Asserts the subject line of the email equals the expected value.
148168

@@ -169,7 +189,7 @@ If your account has [SMS testing](https://mailosaur.com/sms-testing/) enabled, y
169189

170190
```java
171191
@Test public void testSmsExample() throws IOException, MailosaurException {
172-
MailosaurClient mailosaur = new MailosaurClient("API_KEY");
192+
MailosaurClient mailosaur = new MailosaurClient();
173193

174194
String serverId = "abc123";
175195

src/main/java/com/mailosaur/MailosaurClient.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ public void initialize(HttpRequest request) {
3333
}
3434
});
3535

36+
/**
37+
* Initializes an instance of the Mailosaur client using the
38+
* MAILOSAUR_API_KEY environment variable.
39+
*
40+
* @throws MailosaurException if the MAILOSAUR_API_KEY environment variable is not set.
41+
*/
42+
public MailosaurClient() throws MailosaurException {
43+
this(resolveApiKeyFromEnv(), null);
44+
}
45+
3646
/**
3747
* Initializes an instance of the Mailosaur client.
3848
*
@@ -60,6 +70,16 @@ public MailosaurClient(String apiKey, String baseUrl) {
6070
this.devices = new Devices(this);
6171
this.previews = new Previews(this);
6272
}
73+
74+
private static String resolveApiKeyFromEnv() throws MailosaurException {
75+
String apiKey = System.getenv("MAILOSAUR_API_KEY");
76+
if (apiKey == null || apiKey.isEmpty()) {
77+
throw new MailosaurException(
78+
"'apiKey' must be set via the MAILOSAUR_API_KEY environment variable, or passed to the MailosaurClient constructor.",
79+
"authentication_error");
80+
}
81+
return apiKey;
82+
}
6383

6484
/**
6585
* Message analysis operations

0 commit comments

Comments
 (0)