Using API Keys

This guide shows you how to use LangMart API keys in your applications with code examples for various languages and frameworks.

Authentication

All API requests require authentication via the Authorization header:

Authorization: Bearer YOUR_LANGMART_API_KEY

Base URL

All API requests should be sent to:

https://api.langmart.ai/v1

For local development (if running locally):

http://localhost:8081/v1

OpenAI-Compatible API

LangMart provides an OpenAI-compatible API, so you can use existing OpenAI SDKs.

Python with OpenAI SDK

from openai import OpenAI

# Initialize client with LangMart
client = OpenAI(
    api_key="sk-test-your-langmart-key",
    base_url="https://api.langmart.ai/v1"
)

# Make a chat completion request
response = client.chat.completions.create(
    model="openai/gpt-4o",  # Use provider/model format
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ],
    max_tokens=100
)

print(response.choices[0].message.content)

Node.js with OpenAI SDK

import OpenAI from 'openai';

const client = new OpenAI({
    apiKey: process.env.LANGMART_API_KEY,
    baseURL: 'https://api.langmart.ai/v1'
});

async function chat() {
    const response = await client.chat.completions.create({
        model: 'anthropic/claude-3-5-sonnet-20241022',
        messages: [
            { role: 'user', content: 'Hello!' }
        ]
    });

    console.log(response.choices[0].message.content);
}

chat();

cURL

curl https://api.langmart.ai/v1/chat/completions \
  -H "Authorization: Bearer sk-test-your-langmart-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "groq/llama-3.3-70b-versatile",
    "messages": [
      {"role": "user", "content": "Hello!"}
    ]
  }'

Model Naming Convention

LangMart uses the format provider/model-name:

Provider Example Models
OpenAI openai/gpt-4o, openai/gpt-4o-mini
Anthropic anthropic/claude-3-5-sonnet-20241022, anthropic/claude-3-opus-20240229
Groq groq/llama-3.3-70b-versatile, groq/mixtral-8x7b-32768
Google google/gemini-pro, google/gemini-1.5-flash
Together together/meta-llama/Llama-3-70b-chat-hf

Listing Available Models

# List all models available to you
models = client.models.list()

for model in models.data:
    print(f"{model.id}")
# Via cURL
curl https://api.langmart.ai/v1/models \
  -H "Authorization: Bearer YOUR_KEY"

Streaming Responses

For real-time responses, use streaming:

Python Streaming

from openai import OpenAI

client = OpenAI(
    api_key="sk-test-your-langmart-key",
    base_url="https://api.langmart.ai/v1"
)

stream = client.chat.completions.create(
    model="openai/gpt-4o",
    messages=[{"role": "user", "content": "Write a story about a robot."}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

Node.js Streaming

import OpenAI from 'openai';

const client = new OpenAI({
    apiKey: process.env.LANGMART_API_KEY,
    baseURL: 'https://api.langmart.ai/v1'
});

async function streamChat() {
    const stream = await client.chat.completions.create({
        model: 'openai/gpt-4o',
        messages: [{ role: 'user', content: 'Write a story about a robot.' }],
        stream: true
    });

    for await (const chunk of stream) {
        const content = chunk.choices[0]?.delta?.content || '';
        process.stdout.write(content);
    }
}

streamChat();

Environment Variables

Store your API key in environment variables:

Setting Environment Variables

Linux/macOS:

export LANGMART_API_KEY="sk-test-your-key"

Windows (PowerShell):

$env:LANGMART_API_KEY = "sk-test-your-key"

Windows (CMD):

set LANGMART_API_KEY=sk-test-your-key

Using .env Files

Create a .env file (add to .gitignore!):

LANGMART_API_KEY=sk-test-your-key
LANGMART_BASE_URL=https://api.langmart.ai/v1

Python with python-dotenv:

from dotenv import load_dotenv
import os

load_dotenv()

api_key = os.getenv("LANGMART_API_KEY")

Node.js with dotenv:

require('dotenv').config();

const apiKey = process.env.LANGMART_API_KEY;

Common Use Cases

Chat Application

from openai import OpenAI

client = OpenAI(
    api_key=os.environ["LANGMART_API_KEY"],
    base_url="https://api.langmart.ai/v1"
)

def chat(conversation_history, user_message):
    conversation_history.append({
        "role": "user",
        "content": user_message
    })

    response = client.chat.completions.create(
        model="openai/gpt-4o",
        messages=conversation_history
    )

    assistant_message = response.choices[0].message.content
    conversation_history.append({
        "role": "assistant",
        "content": assistant_message
    })

    return assistant_message

# Usage
history = [{"role": "system", "content": "You are a helpful assistant."}]
print(chat(history, "What is Python?"))
print(chat(history, "Show me an example."))

Text Embedding

response = client.embeddings.create(
    model="openai/text-embedding-3-small",
    input="Hello, world!"
)

embedding = response.data[0].embedding
print(f"Embedding dimension: {len(embedding)}")

Image Generation

response = client.images.generate(
    model="openai/dall-e-3",
    prompt="A sunset over mountains",
    size="1024x1024",
    quality="standard",
    n=1
)

image_url = response.data[0].url
print(f"Generated image: {image_url}")

Function Calling

import json

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get the current weather in a location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state, e.g. San Francisco, CA"
                    }
                },
                "required": ["location"]
            }
        }
    }
]

response = client.chat.completions.create(
    model="openai/gpt-4o",
    messages=[{"role": "user", "content": "What's the weather in Paris?"}],
    tools=tools,
    tool_choice="auto"
)

# Check if the model wants to call a function
if response.choices[0].message.tool_calls:
    tool_call = response.choices[0].message.tool_calls[0]
    print(f"Function: {tool_call.function.name}")
    print(f"Arguments: {tool_call.function.arguments}")

Rate Limits

LangMart enforces rate limits to ensure fair usage:

Metric Typical Limit
Requests per minute (RPM) 60-600 (varies by plan)
Tokens per minute (TPM) 40,000-400,000

Handling Rate Limits

import time
from openai import OpenAI, RateLimitError

client = OpenAI(
    api_key=os.environ["LANGMART_API_KEY"],
    base_url="https://api.langmart.ai/v1"
)

def make_request_with_retry(messages, max_retries=3):
    for attempt in range(max_retries):
        try:
            return client.chat.completions.create(
                model="openai/gpt-4o",
                messages=messages
            )
        except RateLimitError as e:
            if attempt < max_retries - 1:
                wait_time = 2 ** attempt  # Exponential backoff
                print(f"Rate limited. Waiting {wait_time}s...")
                time.sleep(wait_time)
            else:
                raise e

Error Handling

Common Errors

Status Code Error Type Description
401 authentication_error Invalid or missing API key
403 permission_error Key lacks required permissions
404 not_found_error Model or resource not found
429 rate_limit_error Rate limit exceeded
500 internal_error Server error

Python Error Handling

from openai import OpenAI, APIError, AuthenticationError, RateLimitError

client = OpenAI(
    api_key=os.environ["LANGMART_API_KEY"],
    base_url="https://api.langmart.ai/v1"
)

try:
    response = client.chat.completions.create(
        model="openai/gpt-4o",
        messages=[{"role": "user", "content": "Hello!"}]
    )
except AuthenticationError:
    print("Invalid API key. Check your LANGMART_API_KEY.")
except RateLimitError:
    print("Rate limit exceeded. Wait and retry.")
except APIError as e:
    print(f"API error: {e.message}")

Node.js Error Handling

import OpenAI from 'openai';

const client = new OpenAI({
    apiKey: process.env.LANGMART_API_KEY,
    baseURL: 'https://api.langmart.ai/v1'
});

try {
    const response = await client.chat.completions.create({
        model: 'openai/gpt-4o',
        messages: [{ role: 'user', content: 'Hello!' }]
    });
} catch (error) {
    if (error instanceof OpenAI.AuthenticationError) {
        console.error('Invalid API key');
    } else if (error instanceof OpenAI.RateLimitError) {
        console.error('Rate limit exceeded');
    } else if (error instanceof OpenAI.APIError) {
        console.error(`API error: ${error.message}`);
    }
}

Response Format

Chat Completion Response

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1704723600,
  "model": "openai/gpt-4o",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Hello! How can I help you today?"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 10,
    "completion_tokens": 12,
    "total_tokens": 22
  }
}

Accessing Response Data

response = client.chat.completions.create(
    model="openai/gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}]
)

# Get the response text
content = response.choices[0].message.content

# Get usage statistics
prompt_tokens = response.usage.prompt_tokens
completion_tokens = response.usage.completion_tokens
total_tokens = response.usage.total_tokens

print(f"Response: {content}")
print(f"Tokens used: {total_tokens}")

Next Steps