Back to Baml

Openapi Howto Rely On Envvars

fern/snippets/openapi-howto-rely-on-envvars.mdx

0.222.02.8 KB
Original Source
<Tabs> <Tab title="Go" language="go">
go
import (
    "os"
    baml "my-golang-app/baml_client"
)

func main() {
    cfg := baml.NewConfiguration()
    if boundaryEndpoint := os.Getenv("BOUNDARY_ENDPOINT"); boundaryEndpoint != "" {
        cfg.BasePath = boundaryEndpoint
    }
    if boundaryApiKey := os.Getenv("BOUNDARY_API_KEY"); boundaryApiKey != "" {
        cfg.DefaultHeader["Authorization"] = "Bearer " + boundaryApiKey
    }
    b := baml.NewAPIClient(cfg).DefaultAPI
    // Use `b` to make API calls
}
</Tab> <Tab title="Java" language="java"> ```java import com.boundaryml.baml_client.ApiClient; import com.boundaryml.baml_client.ApiException; import com.boundaryml.baml_client.Configuration; import com.boundaryml.baml_client.api.DefaultApi; import com.boundaryml.baml_client.auth.*;

public class ApiExample { public static void main(String[] args) { ApiClient apiClient = Configuration.getDefaultApiClient();

    String boundaryEndpoint = System.getenv("BOUNDARY_ENDPOINT");
    if (boundaryEndpoint != null && !boundaryEndpoint.isEmpty()) {
        apiClient.setBasePath(boundaryEndpoint);
    }

    String boundaryApiKey = System.getenv("BOUNDARY_API_KEY");
    if (boundaryApiKey != null && !boundaryApiKey.isEmpty()) {
        apiClient.addDefaultHeader("Authorization", "Bearer " + boundaryApiKey);
    }

    DefaultApi apiInstance = new DefaultApi(apiClient);
    // Use `apiInstance` to make API calls
}

}


</Tab>

<Tab title="PHP" language="php">

```php
require_once(__DIR__ . '/vendor/autoload.php');

$config = BamlClient\Configuration::getDefaultConfiguration();

$boundaryEndpoint = getenv('BOUNDARY_ENDPOINT');
$boundaryApiKey = getenv('BOUNDARY_API_KEY');

if ($boundaryEndpoint) {
    $config->setHost($boundaryEndpoint);
}

if ($boundaryApiKey) {
    $config->setAccessToken($boundaryApiKey);
}

$apiInstance = new OpenAPI\Client\Api\DefaultApi(
    new GuzzleHttp\Client(),
    $config
);

// Use `$apiInstance` to make API calls
</Tab> <Tab title="Ruby" language="ruby"> ```ruby require 'baml_client'

api_client = BamlClient::ApiClient.new

boundary_endpoint = ENV['BOUNDARY_ENDPOINT'] if boundary_endpoint api_client.host = boundary_endpoint end

boundary_api_key = ENV['BOUNDARY_API_KEY'] if boundary_api_key api_client.default_headers['Authorization'] = "Bearer #{boundary_api_key}" end b = BamlClient::DefaultApi.new(api_client)

Use b to make API calls

</Tab>

<Tab title="Rust" language="rust">
```rust
let mut config = baml_client::apis::configuration::Configuration::default();
if let Some(base_path) = std::env::var("BOUNDARY_ENDPOINT").ok() {
    config.base_path = base_path;
}
if let Some(api_key) = std::env::var("BOUNDARY_API_KEY").ok() {
    config.bearer_access_token = Some(api_key);
}
// Use `config` to make API calls
</Tab> </Tabs>