Skip to content

Commit 66d1af9

Browse files
feat(client): allow configuring env via system properties
1 parent 6730e8b commit 66d1af9

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

README.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ import com.braintrustdata.api.client.okhttp.BraintrustOkHttpClient;
5353
import com.braintrustdata.api.models.Project;
5454
import com.braintrustdata.api.models.ProjectCreateParams;
5555

56-
// Configures using the `BRAINTRUST_API_KEY` and `BRAINTRUST_BASE_URL` environment variables
56+
// Configures using the `braintrust.apiKey` and `braintrust.baseUrl` system properties
57+
// Or configures using the `BRAINTRUST_API_KEY` and `BRAINTRUST_BASE_URL` environment variables
5758
BraintrustClient client = BraintrustOkHttpClient.fromEnv();
5859

5960
ProjectCreateParams params = ProjectCreateParams.builder()
@@ -64,13 +65,14 @@ Project project = client.projects().create(params);
6465

6566
## Client configuration
6667

67-
Configure the client using environment variables:
68+
Configure the client using system properties or environment variables:
6869

6970
```java
7071
import com.braintrustdata.api.client.BraintrustClient;
7172
import com.braintrustdata.api.client.okhttp.BraintrustOkHttpClient;
7273

73-
// Configures using the `BRAINTRUST_API_KEY` and `BRAINTRUST_BASE_URL` environment variables
74+
// Configures using the `braintrust.apiKey` and `braintrust.baseUrl` system properties
75+
// Or configures using the `BRAINTRUST_API_KEY` and `BRAINTRUST_BASE_URL` environment variables
7476
BraintrustClient client = BraintrustOkHttpClient.fromEnv();
7577
```
7678

@@ -92,18 +94,21 @@ import com.braintrustdata.api.client.BraintrustClient;
9294
import com.braintrustdata.api.client.okhttp.BraintrustOkHttpClient;
9395

9496
BraintrustClient client = BraintrustOkHttpClient.builder()
95-
// Configures using the `BRAINTRUST_API_KEY` and `BRAINTRUST_BASE_URL` environment variables
97+
// Configures using the `braintrust.apiKey` and `braintrust.baseUrl` system properties
98+
Or configures using the `BRAINTRUST_API_KEY` and `BRAINTRUST_BASE_URL` environment variables
9699
.fromEnv()
97100
.apiKey("My API Key")
98101
.build();
99102
```
100103

101104
See this table for the available options:
102105

103-
| Setter | Environment variable | Required | Default value |
104-
| --------- | --------------------- | -------- | ------------------------------ |
105-
| `apiKey` | `BRAINTRUST_API_KEY` | false | - |
106-
| `baseUrl` | `BRAINTRUST_BASE_URL` | true | `"https://api.braintrust.dev"` |
106+
| Setter | System property | Environment variable | Required | Default value |
107+
| --------- | -------------------- | --------------------- | -------- | ------------------------------ |
108+
| `apiKey` | `braintrust.apiKey` | `BRAINTRUST_API_KEY` | false | - |
109+
| `baseUrl` | `braintrust.baseUrl` | `BRAINTRUST_BASE_URL` | true | `"https://api.braintrust.dev"` |
110+
111+
System properties take precedence over environment variables.
107112

108113
> [!TIP]
109114
> Don't create more than one client in the same application. Each client has a connection pool and
@@ -149,7 +154,8 @@ import com.braintrustdata.api.models.Project;
149154
import com.braintrustdata.api.models.ProjectCreateParams;
150155
import java.util.concurrent.CompletableFuture;
151156

152-
// Configures using the `BRAINTRUST_API_KEY` and `BRAINTRUST_BASE_URL` environment variables
157+
// Configures using the `braintrust.apiKey` and `braintrust.baseUrl` system properties
158+
// Or configures using the `BRAINTRUST_API_KEY` and `BRAINTRUST_BASE_URL` environment variables
153159
BraintrustClient client = BraintrustOkHttpClient.fromEnv();
154160

155161
ProjectCreateParams params = ProjectCreateParams.builder()
@@ -167,7 +173,8 @@ import com.braintrustdata.api.models.Project;
167173
import com.braintrustdata.api.models.ProjectCreateParams;
168174
import java.util.concurrent.CompletableFuture;
169175

170-
// Configures using the `BRAINTRUST_API_KEY` and `BRAINTRUST_BASE_URL` environment variables
176+
// Configures using the `braintrust.apiKey` and `braintrust.baseUrl` system properties
177+
// Or configures using the `BRAINTRUST_API_KEY` and `BRAINTRUST_BASE_URL` environment variables
171178
BraintrustClientAsync client = BraintrustOkHttpClientAsync.fromEnv();
172179

173180
ProjectCreateParams params = ProjectCreateParams.builder()

braintrust-java-core/src/main/kotlin/com/braintrustdata/api/core/ClientOptions.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,11 @@ private constructor(
235235
fun timeout(): Timeout = timeout
236236

237237
fun fromEnv() = apply {
238-
System.getenv("BRAINTRUST_BASE_URL")?.let { baseUrl(it) }
239-
System.getenv("BRAINTRUST_API_KEY")?.let { apiKey(it) }
238+
(System.getProperty("braintrust.baseUrl") ?: System.getenv("BRAINTRUST_BASE_URL"))
239+
?.let { baseUrl(it) }
240+
(System.getProperty("braintrust.apiKey") ?: System.getenv("BRAINTRUST_API_KEY"))?.let {
241+
apiKey(it)
242+
}
240243
}
241244

242245
/**

0 commit comments

Comments
 (0)