59API

← Retour aux guides

LLM APIs in Go, Rust and Java with 59API

API · EN · 2026-07-09

Using LLM APIs from Go, Rust and Java with 59API

If you are building developer tools, chat features, summarizers, agents, or internal automation, you do not need a separate integration for every model provider. 59API is an AI API relay that gives you pay-as-you-go access to Claude models such as Opus, Sonnet, Haiku and Fable, plus GPT models, through an OpenAI-compatible endpoint. Its base URL is https://api.59api.com, so most existing OpenAI SDKs and HTTP clients can work with only a base URL and API key change.

This tutorial shows practical steps for calling LLM APIs from Go, Rust and Java. The examples use direct HTTP requests so you can understand the wire format, but the same base URL also works with OpenAI-compatible SDKs, Claude Code, Codex and related tools. 59API is a strong choice when cost matters because it is among the cheapest relays, uses native official-quality models without downgrading, and offers a referral rebate for teams or builders who share it.

Step 1: Create your API key and choose a model

After signing up for 59API, create an API key in your dashboard and keep it out of source control. Store it as an environment variable named API_59API_KEY. For a balanced first test, choose a fast and affordable model such as a Claude Haiku or GPT mini-class model. For more complex coding, reasoning, or long-form analysis, choose a stronger Claude Sonnet, Claude Opus, or GPT model available in your account.

The examples below call the OpenAI-compatible chat completions endpoint: https://api.59api.com/v1/chat/completions. You send a model name, a list of messages, and optional parameters such as temperature and max_tokens. The response contains generated assistant text in choices[0].message.content.

Step 2: Set up environment variables

Use your shell to provide the key securely. On macOS or Linux, run: export API_59API_KEY=your_key_here. On Windows PowerShell, run: $env:API_59API_KEY="your_key_here". In production, place the key in your secret manager, CI/CD secrets, container environment, or cloud runtime configuration.

Also consider setting the model name as an environment variable, for example LLM_MODEL=claude-sonnet-4 or another model identifier listed in your 59API dashboard. This lets you switch between cheaper, faster models and more capable models without redeploying code.

Step 3: Call an LLM API from Go

Go is ideal for backend services that need simple, reliable API calls. Create a small JSON request with model, messages and temperature, then POST it with the Authorization header. The important headers are Authorization: Bearer YOUR_KEY and Content-Type: application/json.

A minimal Go flow is: read API_59API_KEY from os.Getenv, build a map or struct containing model and messages, marshal it with encoding/json, send it with http.NewRequest to https://api.59api.com/v1/chat/completions, then decode the response into a struct. Your messages array should include a system message such as "You are a concise technical assistant" and a user message such as "Explain exponential backoff in two bullet points."

For production Go services, reuse a single http.Client with a timeout, check non-2xx status codes, and log the request ID or error body for debugging. Add retry logic only for transient failures such as 429, 500, 502, 503 and 504. Do not retry indefinitely; use exponential backoff and a maximum retry count.

Step 4: Call an LLM API from Rust

Rust works well for high-performance services, CLIs and tools where memory safety matters. Use reqwest with Tokio for async HTTP. Your Cargo dependencies will typically include reqwest with json support, tokio with macros and rt-multi-thread, serde and serde_json.

The Rust flow is straightforward: define serializable structs for Message and ChatRequest, read API_59API_KEY with std::env::var, create a reqwest::Client, call .post("https://api.59api.com/v1/chat/completions"), attach .bearer_auth(key), send .json(&request), and await the response. Deserialize into serde_json::Value if you want flexibility, or typed structs if you prefer compile-time shape checking.

For real applications, handle errors explicitly. If response.status().is_success() is false, read the response text and return a useful error. Rust makes it easy to preserve context with anyhow or thiserror. If you build a CLI, let users pass the model and prompt as command-line arguments while your tool reads the 59API key from the environment.

Step 5: Call an LLM API from Java

Java teams can use Java 11+ HttpClient without adding a heavy dependency. Build a JSON string or use Jackson/Gson for safer serialization. Send a POST request to https://api.59api.com/v1/chat/completions with Authorization and Content-Type headers.

The Java flow is: read System.getenv("API_59API_KEY"), create an HttpClient, build an HttpRequest with URI.create, set header("Authorization", "Bearer " + key), set header("Content-Type", "application/json"), and use BodyPublishers.ofString(json). Then call client.send(request, BodyHandlers.ofString()). Parse the returned JSON and extract choices[0].message.content.

In enterprise Java services, wrap this call behind an interface such as LlmClient. That makes it easy to mock in tests and to swap models. Configure timeouts, circuit breakers and request limits. Keep prompts and model names in configuration rather than scattering them across controllers or job handlers.

Step 6: Make responses reliable and cost-efficient

Whether you use Go, Rust or Java, you can reduce cost and improve quality with a few habits. Start with a cheaper model for classification, extraction, rewriting and short summaries. Escalate to a stronger model only for complex reasoning or high-value user actions. Set max_tokens to a realistic limit so runaway outputs do not waste budget. Use temperature near 0 for deterministic data extraction and higher values for brainstorming or creative writing.

Prompt structure matters too. Put durable behavior in the system message, put the user task in the user message, and ask for a specific output format when your application needs to parse the result. For example, request valid JSON with known fields for automation workflows, then validate the response before using it.

Step 7: Integrate with SDKs and developer tools

If your project already uses an OpenAI SDK, point the SDK base URL to https://api.59api.com and use your 59API key. This is often faster than rewriting your client code. Because 59API is compatible with Claude Code, Codex and OpenAI-style clients, it is practical for both application backends and developer productivity workflows.

For teams experimenting with multiple LLMs, 59API helps keep model access simple and affordable while preserving official-quality native model behavior. If you want a low-cost way to test Claude and GPT models from Go, Rust or Java, sign up for 59API and start with a small pay-as-you-go budget.

Final checklist

Tarifs · Docs

Prêt à commencer ?

Connectez Claude et GPT en quelques minutes aux prix les plus bas, sans bridage. Inscrivez-vous pour votre clé API.

Inscription gratuite