59API

← सभी गाइड पर लौटें

Call LLM APIs from Go, Rust, and Java

API · EN · 2026-10-02

1. Set up the API connection

Go, Rust, and Java can all call an LLM through the same basic HTTP workflow: send a POST request with a JSON body, include a bearer token, and read the JSON response. This keeps your integration portable and avoids tying application logic to one provider’s SDK.

With 59API, use https://api.59api.com as the API base URL and store your API key in an environment variable such as LLM_API_KEY. The OpenAI-compatible chat completions endpoint is /v1/chat/completions. For a Claude Messages-style integration, use /v1/messages and follow the request format expected by that endpoint. Check the provider’s current API documentation for supported model IDs and endpoint details.

Start with a small request containing a model, a user message, and a token limit. Keep the key out of source control, logs, and client-side applications. Set a request timeout, and handle non-success HTTP responses before attempting to parse a completion.

2. Call the endpoint from Go

Go’s standard net/http package is enough for a basic integration. Encode the request as JSON, create a POST request, and set both the authorization and content-type headers. For example, your payload can contain model, messages with a user role and prompt content, and max_tokens.

Build the request URL by joining the base URL with /v1/chat/completions, then set the header to Authorization: Bearer followed by the value of LLM_API_KEY. Use an http.Client with a timeout, check the response status, and decode the JSON response body into a struct that matches the fields you need, such as the first choice’s message content. Always close the response body. If you expect large or variable outputs, avoid assuming the response text will fit a fixed-size buffer.

3. Send a request from Rust

In Rust, an asynchronous client such as reqwest works well for service applications. Add reqwest with JSON support and a JSON library such as serde and serde_json. Read the API key from the environment, create a client, and post a serialized request to the same chat completions URL.

Set the bearer token with the client’s bearer-auth option, then call the JSON request builder with a body containing the model and messages. Await the request and check the HTTP status before deserializing the response. Define only the response structs your application needs, or deserialize into a flexible JSON value while prototyping. Propagate network and parsing errors with context instead of silently returning an empty answer. In production, reuse the client rather than constructing one for every prompt.

4. Connect Java with HttpClient

For Java 11 and later, java.net.http.HttpClient provides a built-in option. Create an HttpRequest with a URI ending in /v1/chat/completions, set the POST method, and add the bearer authorization and JSON content-type headers. Serialize a request object with your project’s JSON library, such as Jackson, and send it using HttpClient.send or sendAsync.

Inspect the status code before deserializing the response. Map the completion content into a small application-level result type rather than passing the provider’s entire response object through your codebase. For asynchronous services, use sendAsync and handle exceptional completion as well as HTTP errors.

5. Make the integration reliable and affordable

Keep model selection configurable so you can choose an appropriate Claude or GPT model without changing application code. Use a smaller, less expensive model for straightforward classification or extraction, and reserve more capable models for tasks that need deeper reasoning. Set sensible output-token limits, retry only transient failures with bounded backoff, and avoid automatically retrying requests that may have already been processed unless your workflow accounts for duplicate cost.

Log latency, status codes, and usage data when available, but do not log API keys or sensitive prompts. Start with a short prompt and a low token limit, then measure quality and cost against your actual workload.

59API is a practical option when you want pay-as-you-go access to Claude Opus, Sonnet, Haiku, and Fable, as well as GPT models, through one relay. It advertises native, official-quality models rather than downgraded substitutes, compatibility with Claude Code, Codex, and OpenAI SDKs, and a referral rebate. Its low-cost pricing can make experimentation easier; compare current model pricing and terms with your usage before moving production traffic. You can sign up at 59API to get an API key and try the same request flow in your Go, Rust, or Java service.

मूल्य · दस्तावेज़

शुरू करने के लिए तैयार?

कुछ ही मिनटों में Claude और GPT जोड़ें, सबसे कम कीमत पर। साइन अप करें और API key पाएं।

मुफ़्त साइन अप