A ColdFusion component (CFC) wrapper for the Anthropic Claude API, written in CFScript with proper abstraction and error handling.
- Clean CFScript-based API wrapper
- Environment variable support for API keys
- Proper error handling and structured responses
- Abstracted HTTP requests for maintainability
- Support for both single messages and multi-turn conversations
- Built-in support for all current Claude models
- Clone this repository
- Copy the
claude-cfmlfolder to your CFML application - Set up your API key (see Configuration section)
The component supports multiple ways to provide your Anthropic API key (in order of priority):
- Manual parameter - Pass directly to
init(apiKey) - Environment variable - Set
ANTHROPIC_API_KEY - Java system property - Set
anthropic.api.key
Copy .env.example to .env and add your API key:
cp .env.example .env
# Edit .env and add your actual API key// Initialize the API wrapper
claudeAPI = createObject("component", "claude-cfml.ClaudeAPI").init();
// Or with manual API key
claudeAPI = createObject("component", "claude-cfml.ClaudeAPI").init("your-api-key");result = claudeAPI.sendMessage("Hello, how are you today?");
if (result.success) {
writeOutput(result.response);
} else {
writeOutput("Error: " & result.error);
}result = claudeAPI.sendMessage(
message = "Explain quantum computing",
model = "claude-3-5-sonnet-20241022",
maxTokens = 2048,
temperature = 0.7,
systemPrompt = "You are a helpful physics teacher"
);messages = [
{"role": "user", "content": "What is the capital of France?"},
{"role": "assistant", "content": "The capital of France is Paris."},
{"role": "user", "content": "What's the population?"}
];
result = claudeAPI.sendConversation(messages);Send a single message to Claude.
Parameters:
message(required string) - The message to sendmodel(string) - Claude model to use (default: claude-3-5-sonnet-20241022)maxTokens(numeric) - Maximum tokens in response (default: 1024)temperature(numeric) - Response randomness 0-2 (default: 1)systemPrompt(string) - System prompt to set context
Returns: Struct with success, response, usage, model, and rawResponse keys
Send a multi-turn conversation to Claude.
Parameters:
messages(required array) - Array of message objects withroleandcontentmodel(string) - Claude model to use (default: claude-3-5-sonnet-20241022)maxTokens(numeric) - Maximum tokens in response (default: 1024)temperature(numeric) - Response randomness 0-2 (default: 1)systemPrompt(string) - System prompt to set context
Returns: Struct with success, response, usage, model, and rawResponse keys
Get array of available Claude models.
Returns: Array of model strings
claude-3-5-sonnet-20241022(default)claude-3-5-haiku-20241022claude-3-opus-20240229claude-3-sonnet-20240229claude-3-haiku-20240307
All methods return a struct with a success boolean. On error:
{
"success": false,
"error": "Error message",
"errorDetail": "Detailed error information"
}This project follows the guidelines in CLAUDE.md. Key principles:
- CFScript syntax in CFC files
- Bracket notation for struct keys
- Proper var scoping and argument scoping
- Abstracted HTTP requests
- Environment variable support
A test interface is available at index.cfm when running with CommandBox:
box start cfengine=lucee@6This project is provided as-is for educational and development purposes.