Skip to content

🪢 Auto-generated Java Client for Langfuse API

License

Notifications You must be signed in to change notification settings

langfuse/langfuse-java

Repository files navigation

langfuse-java

This repository contains an auto-generated Langfuse API client for Java based on our API specification. See the Langfuse API reference for more details on the available endpoints.

We recommend to solve tracing via the OpenTelemetry Instrumentation instead of using the Ingestion API directly. This allows for a more detailed and standardized tracing experience without the need to handle batching and updates internally. Check out our Spring AI Example for more details.

Installation

Add the langfuse-java API client as a dependency using

<dependency>
    <groupId>com.langfuse</groupId>
    <artifactId>langfuse-java</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

to fetch our GitHub package.

If you're not scanning the GitHub Package Registry by default, you'll have to add

<repositories>
    <repository>
        <id>github</id>
        <name>GitHub Package Registry</name>
        <url>https://maven.pkg.github.com/langfuse/langfuse-java</url>
    </repository>
</repositories>

as well.

Usage

Instantiate the Langfuse Client with the respective endpoint and your API Keys.

import com.langfuse.client.LangfuseClient;

LangfuseClient client = LangfuseClient.builder()
        .url("https://cloud.langfuse.com") // 🇪🇺 EU data region
        // .url("https://us.cloud.langfuse.com") // 🇺🇸 US data region
        // .url("http://localhost:3000") // 🏠 Local deployment
        .credentials("pk-lf-...", "sk-lf-...")
        .build();

Make requests using the clients:

import com.langfuse.client.core.LangfuseClientApiException;
import com.langfuse.client.resources.prompts.types.PromptMetaListResponse;

try {
    PromptMetaListResponse prompts = client.prompts().list();
} catch (LangfuseClientApiException error) {
    System.out.println(error.getBody());
    System.out.println(error.getStatusCode());
}

Drafting a Release

Run ./mvnw release:prepare -DreleaseVersion= with the version you want to create. Push the changes including the tag.

Updating

  1. Ensure that langfuse-java is placed in the same directory as the main langfuse repository.
  2. Setup a new Java fern generator using
       - name: fernapi/fern-java-sdk
         version: 2.20.1
         output:
           location: local-file-system
           path: ../../../../langfuse-java/src/main/java/com/langfuse/client/
         config:
           client-class-name: LangfuseClient
  3. Generate the new client code using npx fern-api generate --api server.
  4. Manually set the package across all files to com.langfuse.client.
  5. Overwrite this.clientOptionsBuilder.addHeader("Authorization", "Bearer " + encodedToken); to Basic in LangfuseClientBuilder.java.
  6. Commit the changes in langfuse-java and push them to the repository.