API Guide
Understand how to use Polylith APIs and seamlessly integrate with AI providers like OpenAI, Claude, Gemini, and more to power your workflows.
Polylith offers a unified API to simplify the development of AI workflows and agents. This guide explains how to:
Use Polylith APIs to manage workflows and agents.
Integrate External APIs from AI providers like OpenAI, Anthropic, Google, and more.
1. Polylith API
The Polylith API is your starting point for building and managing workflows.
Base URL
Copy
Copy
plaintextCopy codehttps://api.polylith.dev/v1/
Authentication
Include your Polylith API Key in the header:
Copy
Copy
httpCopy codeAuthorization: Bearer YOUR_POLYLITH_API_KEY
Example: Create an AI Workflow
Copy
Copy
httpCopy codePOST /workflows
Host: api.polylith.dev
Authorization: Bearer YOUR_POLYLITH_API_KEY
Content-Type: application/json
{
"name": "example-workflow",
"input": { "platform": "discord", "parameters": { "token": "YOUR_DISCORD_TOKEN" }},
"processing": { "provider": "openai", "model": "gpt-4", "api_key": "YOUR_OPENAI_KEY" },
"output": { "platform": "discord" }
}
2. External API Providers
Polylith integrates seamlessly with external AI providers via their APIs. Here’s a breakdown of key providers and usage:
OpenAI API
Base URL:
Copy
Copy
plaintextCopy codehttps://api.openai.com/v1/
Authentication: Use your OpenAI API Key in the header:
Copy
Copy
httpCopy codeAuthorization: Bearer YOUR_OPENAI_API_KEY
Request Example: Generating Text with GPT-4
Copy
Copy
httpCopy codePOST /chat/completions
Host: api.openai.com
Authorization: Bearer YOUR_OPENAI_API_KEY
Content-Type: application/json
{
"model": "gpt-4",
"messages": [{"role": "user", "content": "Tell me about Polylith."}],
"max_tokens": 100
}
Response Example:
Copy
Copy
jsonCopy code{
"id": "chatcmpl-abc123",
"choices": [{"message": {"role": "assistant", "content": "Polylith is a full-stack AI library."}}],
"usage": {"total_tokens": 50}
}
Anthropic Claude API
Base URL:
Copy
Copy
plaintextCopy codehttps://api.anthropic.com/v1/
Authentication: Use your Claude API Key in the x-api-key
header:
Copy
Copy
httpCopy codex-api-key: YOUR_ANTHROPIC_API_KEY
Request Example:
Copy
Copy
httpCopy codePOST /messages
Host: api.anthropic.com
x-api-key: YOUR_ANTHROPIC_API_KEY
Content-Type: application/json
{
"model": "claude-2",
"messages": [{"role": "user", "content": "What is Polylith?"}],
"max_tokens": 100
}
Google Gemini API
Base URL:
Copy
Copy
plaintextCopy codehttps://generativelanguage.googleapis.com/v1/
Authentication: Pass the API key in the URL:
Copy
Copy
httpCopy code?key=YOUR_GEMINI_API_KEY
Request Example: Generating Text
Copy
Copy
httpCopy codePOST /models/gemini-1:generateContent
Content-Type: application/json
{
"contents": [{"parts": [{"text": "Explain how Polylith works."}]}]
}
3. Combining Providers with Polylith
Polylith’s modular blocks allow you to seamlessly combine these APIs. For example:
Workflow Example: Sending a query to Claude and using the response to post on Discord.
Copy
Copy
pythonCopy codefrom polylith import InputBlock, ProcessingBlock, OutputBlock
# Input: Fetch user query
input_block = InputBlock("discord", token="DISCORD_TOKEN")
# Processing: Send to Claude API
processing_block = ProcessingBlock("claude", model="claude-2", api_key="CLAUDE_API_KEY")
# Output: Post response back to Discord
output_block = OutputBlock("discord_response", token="DISCORD_TOKEN")
# Run workflow
workflow = input_block >> processing_block >> output_block
workflow.run()
4. Key Benefits of Using Polylith with APIs
Unified Management: Handle multiple AI provider APIs through Polylith workflows.
Ease of Use: No need to write complex API integrations—Polylith simplifies everything.
Flexibility: Use providers like OpenAI, Claude, and Gemini based on your project needs.
Scalability: Deploy workflows seamlessly across platforms like Discord, Telegram, and Twitter.
Conclusion
Polylith acts as the central hub for managing your AI workflows while integrating effortlessly with external APIs. Whether you’re using OpenAI for text generation, Claude for reasoning, or Gemini for multimodal tasks, Polylith makes development faster and simpler.
Start building today with Polylith and your favorite AI providers.
Last updated