Skip to content

Instantly share code, notes, and snippets.

@ashokvarmamatta
Created March 30, 2026 05:50
Show Gist options
  • Select an option

  • Save ashokvarmamatta/0b357aee8109558470e996daff3f97e4 to your computer and use it in GitHub Desktop.

Select an option

Save ashokvarmamatta/0b357aee8109558470e996daff3f97e4 to your computer and use it in GitHub Desktop.
πŸ€– Connect ANY AI to Your App β€” Complete Beginner's Guide (Gemini, OpenAI, Claude, OpenRouter, Groq, Ollama) with copy-paste code examples in JavaScript, Kotlin & Python

πŸ€– Connect ANY AI to Your App β€” The Complete Beginner's Guide

One guide. Every AI provider. Zero confusion. Works with Android, iOS, Web, Desktop β€” any app that can make HTTP requests.


πŸ“‹ Table of Contents

  1. What is an API Key?
  2. Google Gemini (FREE)
  3. OpenAI / ChatGPT
  4. Anthropic Claude
  5. OpenRouter (400+ Models)
  6. Groq (Ultra Fast)
  7. Ollama (Local, Free Forever)
  8. How API Calls Work (Visual)
  9. Code Examples
  10. Multi-Key Failover
  11. Troubleshooting
  12. Security Tips

πŸ”‘ What is an API Key?

Think of an API key like a password for your app to talk to an AI service.

You (App)  ──── API Key ────►  AI Service (Gemini, ChatGPT, etc.)
                                    β”‚
                                    β–Ό
                              AI Response

It's just a long string of characters, like:

  • Gemini: AIzaSyD_xxxxxxxxxxxxxxxxxxxx
  • OpenAI: sk-proj-xxxxxxxxxxxxxxxxxxxx
  • Anthropic: sk-ant-api03-xxxxxxxxxxxx

You get it for free (most providers have free tiers), paste it into your app, and boom β€” your app can talk to AI.


1. πŸ”΅ Google Gemini (FREE β€” Easiest!)

Why Start Here?

  • βœ… Completely FREE (generous limits)
  • βœ… Easiest setup (2 minutes)
  • βœ… Powerful models (Gemini 2.0 Flash, 2.5 Pro)
  • βœ… Text + Image + Audio + Video support

Step-by-Step: Get Your Key

Step 1: Open browser β†’ Go to https://aistudio.google.com
Step 2: Sign in with your Google account
Step 3: Click "Get API Key" (top-left or sidebar)
Step 4: Click "Create API Key"
Step 5: Copy the key (starts with "AIzaSy...")
Step 6: Paste it in your app β†’ Done! πŸŽ‰

Available Models

Model Speed Quality Best For
gemini-2.0-flash ⚑⚑⚑ β˜…β˜…β˜…β˜… General use (RECOMMENDED)
gemini-2.0-flash-lite ⚑⚑⚑⚑ β˜…β˜…β˜… Quick simple tasks
gemini-1.5-flash ⚑⚑⚑ β˜…β˜…β˜…β˜… Balanced
gemini-1.5-pro ⚑⚑ β˜…β˜…β˜…β˜…β˜… Complex reasoning
gemini-2.5-flash-preview-05-20 ⚑⚑⚑ β˜…β˜…β˜…β˜…β˜… Latest & greatest
gemini-2.5-pro-preview-05-06 ⚑ β˜…β˜…β˜…β˜…β˜…+ Most powerful

API Call Format

POST https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=YOUR_KEY

Headers:
  Content-Type: application/json

Body:
{
  "contents": [
    {
      "parts": [
        { "text": "Your question here" }
      ]
    }
  ],
  "generationConfig": {
    "temperature": 0.7,
    "maxOutputTokens": 4096
  }
}

Read the Response

{
  "candidates": [
    {
      "content": {
        "parts": [
          { "text": "The AI's answer is here!" }
        ]
      }
    }
  ]
}

Get the answer: response.candidates[0].content.parts[0].text


2. 🟒 OpenAI / ChatGPT

Get Your Key

Step 1: Go to https://platform.openai.com
Step 2: Sign up / Log in
Step 3: Click your profile icon β†’ "API Keys"
       (or go to https://platform.openai.com/api-keys)
Step 4: Click "Create new secret key"
Step 5: Name it anything (e.g., "My App")
Step 6: Copy the key (starts with "sk-proj-...")
Step 7: ⚠️ SAVE IT NOW β€” you can't see it again!

πŸ’° Pricing: Free $5 credits for new accounts. After that, pay-as-you-go. GPT-4o-mini is very cheap (~$0.15 per 1M tokens).

Available Models

Model Speed Quality Cost
gpt-4o-mini ⚑⚑⚑ β˜…β˜…β˜…β˜… πŸ’² (Cheapest)
gpt-4o ⚑⚑ β˜…β˜…β˜…β˜…β˜… πŸ’²πŸ’²
gpt-4-turbo ⚑⚑ β˜…β˜…β˜…β˜…β˜… πŸ’²πŸ’²πŸ’²
o3-mini ⚑ β˜…β˜…β˜…β˜…β˜…+ πŸ’²πŸ’²πŸ’² (Reasoning)

API Call Format

POST https://api.openai.com/v1/chat/completions

Headers:
  Content-Type: application/json
  Authorization: Bearer YOUR_API_KEY

Body:
{
  "model": "gpt-4o-mini",
  "messages": [
    { "role": "system", "content": "You are a helpful assistant." },
    { "role": "user", "content": "Your question here" }
  ],
  "max_tokens": 1000
}

Read the Response

{
  "choices": [
    {
      "message": {
        "content": "The AI's answer is here!"
      }
    }
  ]
}

Get the answer: response.choices[0].message.content


3. 🟠 Anthropic Claude

Get Your Key

Step 1: Go to https://console.anthropic.com
Step 2: Sign up (may need to add payment method)
Step 3: Go to "API Keys" in the sidebar
Step 4: Click "Create Key"
Step 5: Copy the key (starts with "sk-ant-api03-...")

πŸ’° Pricing: Free $5 credits for new accounts. Claude Haiku is cheapest.

Available Models

Model Speed Quality Cost
claude-haiku-4-5-20251001 ⚑⚑⚑ β˜…β˜…β˜…β˜… πŸ’² (Cheapest)
claude-sonnet-4-6 ⚑⚑ β˜…β˜…β˜…β˜…β˜… πŸ’²πŸ’²
claude-opus-4-6 ⚑ β˜…β˜…β˜…β˜…β˜…+ πŸ’²πŸ’²πŸ’² (Best)

API Call Format

POST https://api.anthropic.com/v1/messages

Headers:
  Content-Type: application/json
  x-api-key: YOUR_API_KEY
  anthropic-version: 2023-06-01

Body:
{
  "model": "claude-haiku-4-5-20251001",
  "max_tokens": 1024,
  "system": "You are a helpful assistant.",
  "messages": [
    { "role": "user", "content": "Your question here" }
  ]
}

Read the Response

{
  "content": [
    {
      "type": "text",
      "text": "The AI's answer is here!"
    }
  ]
}

Get the answer: response.content[0].text


4. 🟣 OpenRouter (400+ Models β€” One Key, ALL AIs!)

Why OpenRouter?

  • βœ… ONE API key β†’ access OpenAI, Claude, Gemini, Llama, Mistral, and 400+ models
  • βœ… Some FREE models available
  • βœ… Same OpenAI-compatible format for ALL models
  • βœ… Pay only for what you use

Get Your Key

Step 1: Go to https://openrouter.ai
Step 2: Sign up with Google/GitHub
Step 3: Go to https://openrouter.ai/keys
Step 4: Click "Create Key"
Step 5: Copy the key (starts with "sk-or-v1-...")

Popular Models on OpenRouter

Model Provider Cost
google/gemini-2.0-flash-exp:free Google FREE
meta-llama/llama-3.1-8b-instruct:free Meta FREE
openai/gpt-4o-mini OpenAI πŸ’²
anthropic/claude-sonnet-4-6 Anthropic πŸ’²πŸ’²
google/gemini-2.5-pro-preview Google πŸ’²πŸ’²

API Call Format (Same as OpenAI!)

POST https://openrouter.ai/api/v1/chat/completions

Headers:
  Content-Type: application/json
  Authorization: Bearer YOUR_OPENROUTER_KEY
  HTTP-Referer: https://your-app-name.com

Body:
{
  "model": "google/gemini-2.0-flash-exp:free",
  "messages": [
    { "role": "user", "content": "Your question here" }
  ]
}

Read the Response (Same as OpenAI!)

{
  "choices": [
    {
      "message": {
        "content": "The AI's answer is here!"
      }
    }
  ]
}

Get the answer: response.choices[0].message.content


5. ⚑ Groq (Ultra Fast β€” Free Tier)

Why Groq?

  • βœ… INSANELY fast (10x faster than others)
  • βœ… Free tier available
  • βœ… Runs Llama, Mixtral, Gemma models
  • βœ… OpenAI-compatible API

Get Your Key

Step 1: Go to https://console.groq.com
Step 2: Sign up with Google/GitHub
Step 3: Go to "API Keys"
Step 4: Click "Create API Key"
Step 5: Copy the key (starts with "gsk_...")

Available Models

Model Speed Best For
llama-3.1-70b-versatile ⚑⚑⚑⚑ General use
llama-3.1-8b-instant ⚑⚑⚑⚑⚑ Quick tasks
mixtral-8x7b-32768 ⚑⚑⚑⚑ Long context
gemma2-9b-it ⚑⚑⚑⚑ Lightweight

API Call Format (OpenAI-Compatible!)

POST https://api.groq.com/openai/v1/chat/completions

Headers:
  Content-Type: application/json
  Authorization: Bearer YOUR_GROQ_KEY

Body:
{
  "model": "llama-3.1-70b-versatile",
  "messages": [
    { "role": "user", "content": "Your question here" }
  ],
  "max_tokens": 1000
}

Response format: Same as OpenAI β†’ response.choices[0].message.content


6. ⚫ Ollama (100% FREE, Runs on Your PC)

Why Ollama?

  • βœ… Completely FREE forever β€” no API key, no limits
  • βœ… Runs on your own computer
  • βœ… Your data never leaves your machine
  • βœ… Hundreds of open-source models

Setup

Step 1: Go to https://ollama.com
Step 2: Download & install for your OS
Step 3: Open terminal/command prompt
Step 4: Run: ollama pull mistral    (downloads a model)
Step 5: It's running! No key needed.

Popular Models

ollama pull mistral          # 7B - Great general purpose
ollama pull llama3.1         # 8B - Meta's best open model
ollama pull codellama        # 7B - Coding specialist
ollama pull gemma2           # 9B - Google's open model
ollama pull phi3             # 3.8B - Microsoft, super small

API Call Format

POST http://127.0.0.1:11434/api/chat

Headers:
  Content-Type: application/json

Body:
{
  "model": "mistral",
  "messages": [
    { "role": "user", "content": "Your question here" }
  ],
  "stream": false
}

No API key needed! Just make sure Ollama is running on your computer.

Read the Response

{
  "message": {
    "content": "The AI's answer is here!"
  }
}

Get the answer: response.message.content


πŸ”„ How API Calls Actually Work

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚             β”‚  HTTPS   β”‚                  β”‚  Magic   β”‚            β”‚
β”‚  YOUR APP   │────────► β”‚   AI Provider    │────────► β”‚  AI Model  β”‚
β”‚             β”‚          β”‚  (Google, etc.)  β”‚          β”‚            β”‚
β”‚             β”‚ ◄─────── β”‚                  β”‚ ◄─────── β”‚            β”‚
β”‚             β”‚  JSON    β”‚                  β”‚  Result  β”‚            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

What happens:
1. Your app sends a POST request with your question + API key
2. The AI provider validates your key
3. The AI model processes your question
4. The provider sends back a JSON response
5. Your app reads the answer from the JSON

The Universal Pattern (ALL providers follow this):

1. URL      β†’ Where to send the request
2. Headers  β†’ Your API key + content type
3. Body     β†’ Your question (JSON format)
4. Response β†’ AI's answer (JSON format)

πŸ“¦ Copy-Paste Code Examples

JavaScript (Web / Node.js)

// πŸ”΅ GEMINI
async function askGemini(question, apiKey) {
  const res = await fetch(
    `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${apiKey}`,
    {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        contents: [{ parts: [{ text: question }] }],
        generationConfig: { temperature: 0.7, maxOutputTokens: 4096 }
      })
    }
  );
  const data = await res.json();
  return data.candidates?.[0]?.content?.parts?.[0]?.text || 'No response';
}

// 🟒 OPENAI / 🟣 OPENROUTER / ⚑ GROQ (all same format!)
async function askOpenAI(question, apiKey, baseUrl = 'https://api.openai.com', model = 'gpt-4o-mini') {
  const res = await fetch(`${baseUrl}/v1/chat/completions`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${apiKey}`
    },
    body: JSON.stringify({
      model: model,
      messages: [{ role: 'user', content: question }],
      max_tokens: 1000
    })
  });
  const data = await res.json();
  return data.choices?.[0]?.message?.content || 'No response';
}

// 🟠 ANTHROPIC
async function askClaude(question, apiKey, model = 'claude-haiku-4-5-20251001') {
  const res = await fetch('https://api.anthropic.com/v1/messages', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'x-api-key': apiKey,
      'anthropic-version': '2023-06-01'
    },
    body: JSON.stringify({
      model: model,
      max_tokens: 1024,
      messages: [{ role: 'user', content: question }]
    })
  });
  const data = await res.json();
  return data.content?.[0]?.text || 'No response';
}

// Usage:
// const answer = await askGemini("What is Kotlin?", "AIzaSy...");
// const answer = await askOpenAI("What is Kotlin?", "sk-proj-...");
// const answer = await askOpenAI("What is Kotlin?", "sk-or-v1-...", "https://openrouter.ai/api", "google/gemini-2.0-flash-exp:free");
// const answer = await askOpenAI("What is Kotlin?", "gsk_...", "https://api.groq.com/openai", "llama-3.1-70b-versatile");
// const answer = await askClaude("What is Kotlin?", "sk-ant-api03-...");

Kotlin (Android)

// Using OkHttp (add to build.gradle: implementation("com.squareup.okhttp3:okhttp:4.12.0"))

import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject

// πŸ”΅ GEMINI
suspend fun askGemini(question: String, apiKey: String, model: String = "gemini-2.0-flash"): String {
    val client = OkHttpClient()
    val json = JSONObject().apply {
        put("contents", org.json.JSONArray().put(
            JSONObject().put("parts", org.json.JSONArray().put(
                JSONObject().put("text", question)
            ))
        ))
        put("generationConfig", JSONObject().apply {
            put("temperature", 0.7)
            put("maxOutputTokens", 4096)
        })
    }
    val request = Request.Builder()
        .url("https://generativelanguage.googleapis.com/v1beta/models/$model:generateContent?key=$apiKey")
        .post(json.toString().toRequestBody("application/json".toMediaType()))
        .build()
    val response = client.newCall(request).execute()
    val body = JSONObject(response.body!!.string())
    return body.getJSONArray("candidates")
        .getJSONObject(0).getJSONObject("content")
        .getJSONArray("parts").getJSONObject(0)
        .getString("text")
}

// 🟒 OPENAI-COMPATIBLE (works for OpenAI, OpenRouter, Groq)
suspend fun askOpenAI(
    question: String,
    apiKey: String,
    baseUrl: String = "https://api.openai.com",
    model: String = "gpt-4o-mini"
): String {
    val client = OkHttpClient()
    val json = JSONObject().apply {
        put("model", model)
        put("messages", org.json.JSONArray().put(
            JSONObject().put("role", "user").put("content", question)
        ))
        put("max_tokens", 1000)
    }
    val request = Request.Builder()
        .url("$baseUrl/v1/chat/completions")
        .addHeader("Authorization", "Bearer $apiKey")
        .post(json.toString().toRequestBody("application/json".toMediaType()))
        .build()
    val response = client.newCall(request).execute()
    val body = JSONObject(response.body!!.string())
    return body.getJSONArray("choices")
        .getJSONObject(0).getJSONObject("message")
        .getString("content")
}

// 🟠 ANTHROPIC
suspend fun askClaude(question: String, apiKey: String, model: String = "claude-haiku-4-5-20251001"): String {
    val client = OkHttpClient()
    val json = JSONObject().apply {
        put("model", model)
        put("max_tokens", 1024)
        put("messages", org.json.JSONArray().put(
            JSONObject().put("role", "user").put("content", question)
        ))
    }
    val request = Request.Builder()
        .url("https://api.anthropic.com/v1/messages")
        .addHeader("x-api-key", apiKey)
        .addHeader("anthropic-version", "2023-06-01")
        .post(json.toString().toRequestBody("application/json".toMediaType()))
        .build()
    val response = client.newCall(request).execute()
    val body = JSONObject(response.body!!.string())
    return body.getJSONArray("content")
        .getJSONObject(0).getString("text")
}

Python

import requests

# πŸ”΅ GEMINI
def ask_gemini(question, api_key, model="gemini-2.0-flash"):
    url = f"https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent?key={api_key}"
    data = {
        "contents": [{"parts": [{"text": question}]}],
        "generationConfig": {"temperature": 0.7, "maxOutputTokens": 4096}
    }
    r = requests.post(url, json=data)
    return r.json()["candidates"][0]["content"]["parts"][0]["text"]

# 🟒 OPENAI-COMPATIBLE (OpenAI, OpenRouter, Groq)
def ask_openai(question, api_key, base_url="https://api.openai.com", model="gpt-4o-mini"):
    url = f"{base_url}/v1/chat/completions"
    headers = {"Authorization": f"Bearer {api_key}"}
    data = {
        "model": model,
        "messages": [{"role": "user", "content": question}],
        "max_tokens": 1000
    }
    r = requests.post(url, json=data, headers=headers)
    return r.json()["choices"][0]["message"]["content"]

# 🟠 ANTHROPIC
def ask_claude(question, api_key, model="claude-haiku-4-5-20251001"):
    url = "https://api.anthropic.com/v1/messages"
    headers = {"x-api-key": api_key, "anthropic-version": "2023-06-01"}
    data = {
        "model": model,
        "max_tokens": 1024,
        "messages": [{"role": "user", "content": question}]
    }
    r = requests.post(url, json=data, headers=headers)
    return r.json()["content"][0]["text"]

πŸ”„ Pro Tip: Multi-Key Failover

If one key hits a rate limit, automatically switch to the next one:

const keys = [
  { provider: 'gemini', key: 'AIzaSy...', model: 'gemini-2.0-flash' },
  { provider: 'openrouter', key: 'sk-or-v1-...', model: 'google/gemini-2.0-flash-exp:free' },
  { provider: 'groq', key: 'gsk_...', model: 'llama-3.1-70b-versatile' },
];

async function askAI(question) {
  for (const entry of keys) {
    try {
      if (entry.provider === 'gemini') {
        return await askGemini(question, entry.key, entry.model);
      } else if (entry.provider === 'openrouter' || entry.provider === 'groq') {
        const baseUrl = entry.provider === 'openrouter'
          ? 'https://openrouter.ai/api'
          : 'https://api.groq.com/openai';
        return await askOpenAI(question, entry.key, baseUrl, entry.model);
      }
    } catch (err) {
      console.log(`${entry.provider} failed, trying next...`);
    }
  }
  throw new Error('All AI providers failed!');
}

❓ Troubleshooting

Error What It Means Fix
401 Unauthorized Wrong API key Double-check your key, no extra spaces
403 Forbidden Key doesn't have access Check billing/permissions on provider dashboard
429 Too Many Requests Rate limited Wait a minute, or use multi-key failover
500 Internal Server Error Provider is down Wait and retry, or switch provider
CORS Error (browser) Browser blocking request Use a backend proxy, or use the API from server-side
Model not found Wrong model name Check the model table above for exact names

Quick Debug Checklist

β–‘ Is the API key correct? (no extra spaces, full key)
β–‘ Is the API key enabled? (not revoked on dashboard)
β–‘ Do you have credits/billing set up?
β–‘ Is the model name spelled correctly?
β–‘ Are you using the right URL for the provider?
β–‘ Is the Content-Type header set to application/json?
β–‘ Is the request body valid JSON?

πŸ”’ Security Tips

⚠️  NEVER put API keys in:
    βœ— Source code committed to GitHub
    βœ— Client-side JavaScript (anyone can see it)
    βœ— Public URLs or logs

βœ…  ALWAYS store API keys in:
    βœ“ Environment variables (.env files)
    βœ“ Android SharedPreferences / DataStore
    βœ“ iOS Keychain
    βœ“ Server-side only (proxy API calls)
    βœ“ localStorage (for personal/prototype apps only)

🎯 Quick Decision Chart

Need FREE AI?
β”œβ”€β”€ Yes, with internet    β†’ Gemini (best free) or OpenRouter (free models)
β”œβ”€β”€ Yes, no internet      β†’ Ollama (runs locally)
└── Budget available?
    β”œβ”€β”€ Want the BEST      β†’ Claude Opus or GPT-4o
    β”œβ”€β”€ Want FAST + CHEAP  β†’ Groq (Llama) or GPT-4o-mini
    β”œβ”€β”€ Want ONE key for ALL β†’ OpenRouter
    └── Want PRIVACY       β†’ Ollama (your data stays local)

πŸ”— Quick Links

Provider Get API Key Docs Pricing
Gemini aistudio.google.com Docs FREE
OpenAI platform.openai.com Docs Pricing
Anthropic console.anthropic.com Docs Pricing
OpenRouter openrouter.ai/keys Docs Per model
Groq console.groq.com Docs FREE tier
Ollama ollama.com Docs FREE forever

Made by @ashokvarmamatta β€” If this helped you, give it a ⭐!

Used in: Prompt Gen Β· ZeroClaw Β· Neural Forge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment