Skip to content

Commit bca0867

Browse files
committed
[STG-1808] Prefer STAGEHAND_API_URL env fallback
1 parent 8dfe6d2 commit bca0867

3 files changed

Lines changed: 51 additions & 15 deletions

File tree

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ import com.browserbase.api.client.StagehandClient;
259259
import com.browserbase.api.client.okhttp.StagehandOkHttpClient;
260260

261261
// Configures using the `stagehand.browserbaseApiKey`, `stagehand.browserbaseProjectId`, `stagehand.modelApiKey` and `stagehand.baseUrl` system properties
262-
// Or configures using the `BROWSERBASE_API_KEY`, `BROWSERBASE_PROJECT_ID`, `MODEL_API_KEY` and `STAGEHAND_BASE_URL` environment variables
262+
// Or configures using the `BROWSERBASE_API_KEY`, `BROWSERBASE_PROJECT_ID`, `MODEL_API_KEY` and `STAGEHAND_API_URL` environment variables
263263
StagehandClient client = StagehandOkHttpClient.fromEnv();
264264
```
265265

@@ -284,7 +284,7 @@ import com.browserbase.api.client.okhttp.StagehandOkHttpClient;
284284

285285
StagehandClient client = StagehandOkHttpClient.builder()
286286
// Configures using the `stagehand.browserbaseApiKey`, `stagehand.browserbaseProjectId`, `stagehand.modelApiKey` and `stagehand.baseUrl` system properties
287-
// Or configures using the `BROWSERBASE_API_KEY`, `BROWSERBASE_PROJECT_ID`, `MODEL_API_KEY` and `STAGEHAND_BASE_URL` environment variables
287+
// Or configures using the `BROWSERBASE_API_KEY`, `BROWSERBASE_PROJECT_ID`, `MODEL_API_KEY` and `STAGEHAND_API_URL` environment variables
288288
.fromEnv()
289289
.browserbaseApiKey("My Browserbase API Key")
290290
.build();
@@ -297,7 +297,7 @@ See this table for the available options:
297297
| `browserbaseApiKey` | `stagehand.browserbaseApiKey` | `BROWSERBASE_API_KEY` | true | - |
298298
| `browserbaseProjectId` | `stagehand.browserbaseProjectId` | `BROWSERBASE_PROJECT_ID` | true | - |
299299
| `modelApiKey` | `stagehand.modelApiKey` | `MODEL_API_KEY` | true | - |
300-
| `baseUrl` | `stagehand.baseUrl` | `STAGEHAND_BASE_URL` | true | `"https://api.stagehand.browserbase.com"` |
300+
| `baseUrl` | `stagehand.baseUrl` | `STAGEHAND_API_URL` | true | `"https://api.stagehand.browserbase.com"` |
301301

302302
System properties take precedence over environment variables.
303303

@@ -346,7 +346,7 @@ import com.browserbase.api.models.sessions.SessionActResponse;
346346
import java.util.concurrent.CompletableFuture;
347347

348348
// Configures using the `stagehand.browserbaseApiKey`, `stagehand.browserbaseProjectId`, `stagehand.modelApiKey` and `stagehand.baseUrl` system properties
349-
// Or configures using the `BROWSERBASE_API_KEY`, `BROWSERBASE_PROJECT_ID`, `MODEL_API_KEY` and `STAGEHAND_BASE_URL` environment variables
349+
// Or configures using the `BROWSERBASE_API_KEY`, `BROWSERBASE_PROJECT_ID`, `MODEL_API_KEY` and `STAGEHAND_API_URL` environment variables
350350
StagehandClient client = StagehandOkHttpClient.fromEnv();
351351

352352
SessionActParams params = SessionActParams.builder()
@@ -366,7 +366,7 @@ import com.browserbase.api.models.sessions.SessionActResponse;
366366
import java.util.concurrent.CompletableFuture;
367367

368368
// Configures using the `stagehand.browserbaseApiKey`, `stagehand.browserbaseProjectId`, `stagehand.modelApiKey` and `stagehand.baseUrl` system properties
369-
// Or configures using the `BROWSERBASE_API_KEY`, `BROWSERBASE_PROJECT_ID`, `MODEL_API_KEY` and `STAGEHAND_BASE_URL` environment variables
369+
// Or configures using the `BROWSERBASE_API_KEY`, `BROWSERBASE_PROJECT_ID`, `MODEL_API_KEY` and `STAGEHAND_API_URL` environment variables
370370
StagehandClientAsync client = StagehandOkHttpClientAsync.fromEnv();
371371

372372
SessionActParams params = SessionActParams.builder()

stagehand-java-core/src/main/kotlin/com/browserbase/api/core/ClientOptions.kt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -416,24 +416,26 @@ private constructor(
416416
* |`browserbaseApiKey` |`stagehand.browserbaseApiKey` |`BROWSERBASE_API_KEY` |true |- |
417417
* |`browserbaseProjectId`|`stagehand.browserbaseProjectId`|`BROWSERBASE_PROJECT_ID`|true |- |
418418
* |`modelApiKey` |`stagehand.modelApiKey` |`MODEL_API_KEY` |true |- |
419-
* |`baseUrl` |`stagehand.baseUrl` |`STAGEHAND_BASE_URL` |true |`"https://api.stagehand.browserbase.com"`|
419+
* |`baseUrl` |`stagehand.baseUrl` |`STAGEHAND_API_URL` |true |`"https://api.stagehand.browserbase.com"`|
420420
*
421421
* System properties take precedence over environment variables.
422422
*/
423-
fun fromEnv() = apply {
424-
(System.getProperty("stagehand.baseUrl") ?: System.getenv("STAGEHAND_BASE_URL"))?.let {
425-
baseUrl(it)
426-
}
427-
(System.getProperty("stagehand.browserbaseApiKey")
428-
?: System.getenv("BROWSERBASE_API_KEY"))
423+
fun fromEnv() = fromEnv(System::getenv)
424+
425+
internal fun fromEnv(getEnv: (String) -> String?) = apply {
426+
(System.getProperty("stagehand.baseUrl")
427+
?: getEnv("STAGEHAND_API_URL")
428+
?: getEnv("STAGEHAND_BASE_URL"))
429+
?.let { baseUrl(it) }
430+
(System.getProperty("stagehand.browserbaseApiKey") ?: getEnv("BROWSERBASE_API_KEY"))
429431
?.let { browserbaseApiKey(it) }
430432
(System.getProperty("stagehand.browserbaseProjectId")
431-
?: System.getenv("BROWSERBASE_PROJECT_ID"))
433+
?: getEnv("BROWSERBASE_PROJECT_ID"))
432434
?.let { browserbaseProjectId(it) }
433-
(System.getProperty("stagehand.modelApiKey") ?: System.getenv("MODEL_API_KEY"))?.let {
435+
(System.getProperty("stagehand.modelApiKey") ?: getEnv("MODEL_API_KEY"))?.let {
434436
modelApiKey(it)
435437
}
436-
System.getenv("STAGEHAND_CUSTOM_HEADERS")?.let { customHeadersEnv ->
438+
getEnv("STAGEHAND_CUSTOM_HEADERS")?.let { customHeadersEnv ->
437439
for (line in customHeadersEnv.split("\n")) {
438440
val colon = line.indexOf(':')
439441
if (colon >= 0) {

stagehand-java-core/src/test/kotlin/com/browserbase/api/core/ClientOptionsTest.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,40 @@ internal class ClientOptionsTest {
4747
.containsExactly("another My Browserbase API Key")
4848
}
4949

50+
@Test
51+
fun fromEnv_prefersStagehandApiUrl() {
52+
val env =
53+
mapOf(
54+
"STAGEHAND_API_URL" to "https://example.com/from-api-env",
55+
"STAGEHAND_BASE_URL" to "https://example.com/from-base-env",
56+
)
57+
val clientOptions =
58+
ClientOptions.builder()
59+
.httpClient(httpClient)
60+
.browserbaseApiKey("My Browserbase API Key")
61+
.browserbaseProjectId("My Browserbase Project ID")
62+
.modelApiKey("My Model API Key")
63+
.fromEnv(env::get)
64+
.build()
65+
66+
assertThat(clientOptions.baseUrl()).isEqualTo("https://example.com/from-api-env")
67+
}
68+
69+
@Test
70+
fun fromEnv_usesLegacyStagehandBaseUrl() {
71+
val env = mapOf("STAGEHAND_BASE_URL" to "https://example.com/from-base-env")
72+
val clientOptions =
73+
ClientOptions.builder()
74+
.httpClient(httpClient)
75+
.browserbaseApiKey("My Browserbase API Key")
76+
.browserbaseProjectId("My Browserbase Project ID")
77+
.modelApiKey("My Model API Key")
78+
.fromEnv(env::get)
79+
.build()
80+
81+
assertThat(clientOptions.baseUrl()).isEqualTo("https://example.com/from-base-env")
82+
}
83+
5084
@Test
5185
fun toBuilder_whenOriginalClientOptionsGarbageCollected_doesNotCloseOriginalClient() {
5286
var clientOptions =

0 commit comments

Comments
 (0)