Skip to main content

Cross-Platform

Kotlin Multiplatform

You have two options for integrating Gateway into your KMP application:

The Gateway SDK provides native Kotlin Multiplatform support with built-in security features like device attestation, API key protection, and certificate pinning.

Setup

  1. Add the Gateway SDK dependency to your shared module's build.gradle.kts:
commonMain.dependencies {
implementation("io.github.brahyam:gateway-client:0.2.0")
}
  1. Configure Gateway in your shared code (e.g., RootComponent, MainViewModel, or app startup):
import io.github.brahyam.gateway.client.Gateway

class RootComponent {
init {
// Replace with your actual Google Cloud Project Number
Gateway.configure(
googleCloudProjectNumber = YOUR_GCP_PROJECT_NUMBER
)
}
}
  1. Create a service instance using your Gateway credentials:
import io.github.brahyam.gateway.client.Gateway

// Gateway-Protected Service (Recommended)
val openAIService = Gateway.createOpenAIService(
partialKey = "your-partial-key", // Get this from the Gateway dashboard
serviceURL = "your-service-url" // Get this from the Gateway dashboard
)

// Or Direct Service (for testing only - exposes API keys)
val directOpenAIService = Gateway.createDirectOpenAIService(
apiKey = "your-openai-api-key"
)
  1. Make API requests:
val response = openAIService.chatCompletion(
request = ChatCompletionRequest(
model = ModelId("gpt-4o-mini"),
messages = listOf(ChatMessage(role = Role.User, content = "Hello, how are you?"))
)
)
println(response.choices[0].message.content)

Supported AI Providers

The Gateway SDK supports multiple AI service providers:

  • OpenAI
  • Google Gemini
  • Groq
  • Mistral AI
  • Together AI
  • Anthropic Claude
  • AI/ML API
  • Custom (any compatible OpenAI-style API)

Option 2: Use Your Existing SDK

Device Attestation Not Available

When using your existing SDK instead of the Gateway SDK, device attestation will not be available. This means you'll lose the security benefits of verifying that requests come from genuine, untampered devices. For maximum security, we recommend using the Gateway SDK (Option 1).

You can use Gateway to route your API calls without changing your current SDK.

OpenAI API client for Kotlin

com.aallam.openai:openai-client

Follow the official documentation to install the library.

Then, when initializing the client, pass in your Gateway API key and endpoint instead of defaults:

val openAi = OpenAI(
token = "YOUR_GATEWAY_API_KEY",
host = OpenAIHost("https://api.meetgateway.com/v1/proxy/YOUR_ENDPOINT/v1")
)
warning

Remember to suffix your endpoint with /v1 to match the OpenAI API version as the library does not include it when using an alternative host.

Ktor Client

val client = HttpClient(CIO) {
defaultRequest {
url("https://api.meetgateway.com/v1/proxy/YOUR_ENDPOINT/")
header("Authorization", "Bearer YOUR_GATEWAY_API_KEY")
}
}