BoxLang 🚀 A New JVM Dynamic Language Learn More...
A ColdFusion component (CFC) wrapper for the Anthropic Claude API, written in CFScript with proper abstraction and error handling.
claude-cfml
folder to your CFML applicationThe component supports multiple ways to provide your Anthropic API key (in order of priority):
init(apiKey)
ANTHROPIC_API_KEY
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);
sendMessage()
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
sendConversation()
Send a multi-turn conversation to Claude.
Parameters:
messages
(required array) - Array of message objects
with role
and content
model
(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
getModels()
Get array of available Claude models.
Returns: Array of model strings
claude-3-5-sonnet-20241022
(default)claude-3-5-haiku-20241022
claude-3-opus-20240229
claude-3-sonnet-20240229
claude-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:
A test interface is available at index.cfm
when running
with CommandBox:
box start cfengine=lucee@6
This project is provided as-is for educational and development purposes.
$
box install claude-cfml