How to use gRPC to call Gemini APIs: Configuration & Authentication Help Needed

Hello,
is it possible to call the generativelanguage APIs using gRPC?

I’m exploring using the generativelanguage APIs with gRPC in a Swift project, and I’m running into some configuration issues. Specifically, I’m getting the “Channel isn’t ready.” error, and I’m unclear on how to correctly configure the host, port, and API key for gRPC calls.

I’ve generated Swift code from the proto files, but I’m struggling to determine:

  • The correct host address and port to use for the generativelanguage gRPC endpoint. Is there a specific gRPC endpoint published for these APIs? Do we include v1beta etc. in the URL?
  • The proper method for authenticating my gRPC requests with an API key. I’m not seeing explicit examples of how to add the API key in the gRPC context.

I’ve created a minimal example demonstrating my setup, including the generated Swift code. You can find it in this repository:

GenerativeLanguageTests.swift

Trying evans CLI

evans --tls --host generativelanguage.googleapis.com -r repl

evans: failed to run REPL mode: failed to instantiate a new spec: failed to instantiate the spec: failed to list packages by gRPC reflection: failed to list services from reflection enabled gRPC server: rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing dial tcp [2a00:1450:4001:82f::200a]:50051: i/o timeout"
1 Like

Welcome to the forum. The REST API is the best source for this information, see here - Generating content  |  Gemini API  |  Google AI for Developers
Make sure to select ‘Shell’ for the examples.

Hope that helps.

1 Like

Don’t know,maybe some developers know about that.

1 Like

Thank you! I’m familiar with the REST API, but considering all the compelling reasons Google highlights about gRPC, such as its speed and code generation capabilities based on Google’s official proto files, I want to opt for gRPC.

Another reason was that the official Swift SDK, which relies on the JSON API, is not available for Linux.

1 Like