Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,40 @@ docker compose run --rm -e SPRING_PROFILES_ACTIVE=dev,seed app
./mvnw compile exec:java -Dexec.mainClass=finance.idem.examples.basic.BasicTransactionExampleKt
```

### Configuring `.env`

Every example reads two variables — `IDEM_BASE_URL` and `IDEM_API_KEY` — via a
shared `requiredEnv()` helper
([`support/Env.kt`](src/main/kotlin/finance/idem/examples/support/Env.kt))
backed by [`dotenv-kotlin`](https://github.com/cdimascio/dotenv-kotlin). It
loads `.env` from the project root automatically, so once the file is filled
in you can run any example directly — no manual `export`/`source` step
needed. A real environment variable, if set, always takes precedence over
the value in `.env`.

`.env` is gitignored; `.env.example` is the checked-in template:

```bash
cp .env.example .env
```

Then fill in:

| Variable | Value |
|---|---|
| `IDEM_BASE_URL` | `http://localhost:8081` when running the stack via `docker compose up -d` above |
| `IDEM_API_KEY` | The key printed by the `docker compose run --rm -e SPRING_PROFILES_ACTIVE=dev,seed app` seed step |

```bash
# .env
IDEM_BASE_URL=http://localhost:8081
IDEM_API_KEY=sk_live_... # from the seed step's printed output
```

If `.env` is missing or a variable is blank, each example fails fast with
`IDEM_BASE_URL is not set — see .env.example` (or the equivalent for
`IDEM_API_KEY`) rather than a confusing SDK-level error.

## Examples

| # | Example | What it shows |
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
<artifactId>kotlinx-coroutines-core</artifactId>
<version>1.7.3</version>
</dependency>
<dependency>
<groupId>io.github.cdimascio</groupId>
<artifactId>dotenv-kotlin</artifactId>
<version>6.5.1</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package finance.idem.examples.basic

import finance.idem.examples.support.createAccount
import finance.idem.examples.support.requiredEnv
import finance.idem.sdk.IdemClient
import finance.idem.sdk.model.EntryType
import finance.idem.sdk.model.FiatCurrency
Expand All @@ -24,8 +25,8 @@ import java.util.UUID
*/
fun main() =
runBlocking {
val baseUrl = System.getenv("IDEM_BASE_URL") ?: error("IDEM_BASE_URL is not set — see .env.example")
val apiKey = System.getenv("IDEM_API_KEY") ?: error("IDEM_API_KEY is not set — see .env.example")
val baseUrl = requiredEnv("IDEM_BASE_URL")
val apiKey = requiredEnv("IDEM_API_KEY")

val client = IdemClient(baseUrl = baseUrl, apiKey = apiKey)
client.use {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
import finance.idem.examples.support.allowAgentMaxDebitPerSession
import finance.idem.examples.support.createAccount
import finance.idem.examples.support.mintAgentApiKey
import finance.idem.examples.support.requiredEnv
import finance.idem.sdk.IdemClient
import io.modelcontextprotocol.client.McpClient
import io.modelcontextprotocol.client.transport.HttpClientSseClientTransport
Expand Down Expand Up @@ -40,8 +41,8 @@ import java.util.UUID
*/
fun main() =
runBlocking {
val baseUrl = System.getenv("IDEM_BASE_URL") ?: error("IDEM_BASE_URL is not set — see .env.example")
val apiKey = System.getenv("IDEM_API_KEY") ?: error("IDEM_API_KEY is not set — see .env.example")
val baseUrl = requiredEnv("IDEM_BASE_URL")
val apiKey = requiredEnv("IDEM_API_KEY")
val objectMapper = ObjectMapper()

val client = IdemClient(baseUrl = baseUrl, apiKey = apiKey)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package finance.idem.examples.onchain

import finance.idem.examples.support.createAccount
import finance.idem.examples.support.requiredEnv
import finance.idem.sdk.IdemClient
import finance.idem.sdk.model.ChainId
import finance.idem.sdk.model.EntryType
Expand Down Expand Up @@ -39,8 +40,8 @@ import java.util.UUID
*/
fun main() =
runBlocking {
val baseUrl = System.getenv("IDEM_BASE_URL") ?: error("IDEM_BASE_URL is not set — see .env.example")
val apiKey = System.getenv("IDEM_API_KEY") ?: error("IDEM_API_KEY is not set — see .env.example")
val baseUrl = requiredEnv("IDEM_BASE_URL")
val apiKey = requiredEnv("IDEM_API_KEY")

val client = IdemClient(baseUrl = baseUrl, apiKey = apiKey)
client.use {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package finance.idem.examples.reconciliation

import finance.idem.examples.support.createAccount
import finance.idem.examples.support.requiredEnv
import finance.idem.sdk.IdemClient
import finance.idem.sdk.model.ChainId
import finance.idem.sdk.model.EntryType
Expand All @@ -27,8 +28,8 @@ import java.util.UUID
*/
fun main() =
runBlocking {
val baseUrl = System.getenv("IDEM_BASE_URL") ?: error("IDEM_BASE_URL is not set — see .env.example")
val apiKey = System.getenv("IDEM_API_KEY") ?: error("IDEM_API_KEY is not set — see .env.example")
val baseUrl = requiredEnv("IDEM_BASE_URL")
val apiKey = requiredEnv("IDEM_API_KEY")

val client = IdemClient(baseUrl = baseUrl, apiKey = apiKey)
client.use {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package finance.idem.examples.settlement

import finance.idem.examples.support.createAccount
import finance.idem.examples.support.requiredEnv
import finance.idem.sdk.IdemClient
import finance.idem.sdk.model.ChainId
import finance.idem.sdk.model.EntryType
Expand Down Expand Up @@ -29,8 +30,8 @@ import java.util.UUID
*/
fun main() =
runBlocking {
val baseUrl = System.getenv("IDEM_BASE_URL") ?: error("IDEM_BASE_URL is not set — see .env.example")
val apiKey = System.getenv("IDEM_API_KEY") ?: error("IDEM_API_KEY is not set — see .env.example")
val baseUrl = requiredEnv("IDEM_BASE_URL")
val apiKey = requiredEnv("IDEM_API_KEY")

val client = IdemClient(baseUrl = baseUrl, apiKey = apiKey)
client.use {
Expand Down
17 changes: 17 additions & 0 deletions src/main/kotlin/finance/idem/examples/support/Env.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package finance.idem.examples.support

import io.github.cdimascio.dotenv.dotenv

/**
* Loaded once per JVM. Host environment variables always take precedence
* over `.env` entries (dotenv-kotlin's own resolution order), so a real
* shell export still wins over a stale `.env` file.
*/
private val dotenv = dotenv { ignoreIfMissing = true }

/**
* Resolves an environment variable from the host environment or `.env`
* (see `.env.example`), failing fast with a message pointing at the file
* every example relies on for local configuration.
*/
fun requiredEnv(key: String): String = dotenv[key] ?: error("$key is not set — see .env.example")