LangChain + APIMaster.ai
Use APIMaster.ai OpenAI-compatible API in LangChain instead of the official OpenAI quickstart key.
LangChain is a popular framework for LLM applications. APIMaster.ai exposes an OpenAI-compatible API — set model_provider="openai" and point base_url at APIMaster.
Get an API Key first. Copy the exact model id from the marketplace (e.g.
gpt-5.4,claude-sonnet-4-6).
Prerequisites
- Python 3.10+ (3.11+ recommended).
- An APIMaster API Key from the console.
- A target model id from the marketplace.
Step 1 — Install dependencies
pip install langchain langchain-openai httpx
Step 2 — Create the sample file
Create apimaster_quickstart.py:
import httpx
from langchain.agents import create_agent
from langchain.chat_models import init_chat_model
APIMASTER_API_KEY = "your APIMaster.ai key"
APIMASTER_BASE_URL = "https://apimaster.ai/v1"
MODEL_NAME = "gpt-5.4"
def get_weather(city: str) -> str:
"""Get weather for a given city."""
return f"It's always sunny in {city}!"
def main() -> None:
model = init_chat_model(
MODEL_NAME,
model_provider="openai",
api_key=APIMASTER_API_KEY,
base_url=APIMASTER_BASE_URL,
http_client=httpx.Client(trust_env=False, timeout=60),
timeout=60,
)
agent = create_agent(
model=model,
tools=[get_weather],
system_prompt="You are a helpful assistant",
)
result = agent.invoke(
{"messages": [{"role": "user", "content": "What's the weather in San Francisco?"}]}
)
print(result["messages"][-1].content_blocks)
if __name__ == "__main__":
main()
You can also download the sample script and paste your key before running.
Step 3 — Run
python apimaster_quickstart.py
On success you should see output similar to:
[{'type': 'text', 'text': "It's always sunny in San Francisco!"}]
The agent calls the get_weather tool and returns the final reply.
Key settings
APIMaster OpenAI-compatible base URL:
https://apimaster.ai/v1
Core LangChain configuration:
model = init_chat_model(
"gpt-5.4",
model_provider="openai",
api_key=APIMASTER_API_KEY,
base_url="https://apimaster.ai/v1",
)
| Parameter | Value |
|---|---|
model_provider |
"openai" (OpenAI-compatible protocol) |
base_url |
https://apimaster.ai/v1 |
| Model name | Marketplace model id |
GPT example: MODEL_NAME = "gpt-5.4"
Claude example: MODEL_NAME = "claude-sonnet-4-6"
Proxy issues
If HTTP_PROXY / HTTPS_PROXY is set locally, you may hit SSL or connection errors. Pass:
http_client=httpx.Client(trust_env=False, timeout=60)
This stops httpx from picking up system proxy env vars — useful for quick local tests. Configure proxies explicitly in production if needed.
Safer key handling
For real projects, use environment variables instead of hard-coding keys:
import os
APIMASTER_API_KEY = os.environ["APIMASTER_API_KEY"]
APIMASTER_BASE_URL = "https://apimaster.ai/v1"
export APIMASTER_API_KEY="your key"
python apimaster_quickstart.py
Windows PowerShell:
$env:APIMASTER_API_KEY="your key"
python apimaster_quickstart.py
Troubleshooting
| Symptom | Fix |
|---|---|
| 401 / Invalid API Key | Verify key is complete and enabled in console |
| 404 / model not found | MODEL_NAME must match marketplace model id exactly |
| SSL / timeout | Try trust_env=False; check firewall/proxy |
ModuleNotFoundError |
Run pip install langchain langchain-openai httpx |
Checklist
- Installed
langchain,langchain-openai,httpx -
base_url=https://apimaster.ai/v1 -
MODEL_NAMEfrom marketplace - API Key set (code or env var)
-
apimaster_quickstart.pyruns and prints agent output