npm install claude-ai
First, import the library:
const Claude = require("claude-ai");
Initialize a new Claude instance with your session key:
Note
GetsessionKey
from thesessionKey
cookie via the Claude website.
const claude = new Claude({
sessionKey: "YOUR_SESSION_KEY",
});
The main class for interfacing with the Claude API.
const claude = new Claude({
sessionKey: string,
proxy: string | function
})
sessionKey
<string> - Your ClaudesessionKey
cookieproxy
<string | function>- If
proxy
is a string, it will be prepended before the API endpoint, example:https://claude.ai/
- If
proxy
is a function, it will be passed the API route to fetch as well as the fetch options which can then be manipulated before running through fetch.
- If
startConversation(prompt, params)
<Promise<Conversation>> - Starts a new conversation with the given prompt messageprompt
<string> - The initial prompt for the conversationparams
<object> - The parameters to pass to the initialsendMessage
call.
getConversations()
<Promise<Conversation[]>> - Gets recent conversationsclearConversations()
<Promise<Response[]>> - Clear all conversationsuploadFile(file)
<Promise<Attachment>> - Uploads a filefile
<File> - File object to upload
Returned by Claude.startConversation()
.
-
sendMessage(message, options)
<Promise<MessageStream>> - Sends a followup message in the conversationmessage
<string> - The message to sendoptions
<object>timezone
<string> - The timezone for completion (default:"America/New_York"
)attachments
<File[]> - Array of file attachmentsmodel
<string> - The Claude model to use (default:"claude-2"
)done
<(MessageStream) => void> - Callback when completedprogress
<(MessageStream) => void> - Progress callback from the progress, a block of text where each line starts with "data:" then has some JSON.rawResponse
<function> Passed the raw response text
-
getInfo()
<Promise<Conversation>> - Gets the conversation info (includes messages, name, created_at, updated_at, etc) -
delete()
<Promise<Response>> - Delete the conversation -
rename(title)
<Promise<Response>> - Rename a conversation -
retryMessage()
<Promise<MessageStream>> - Retry the last message in the conversation (claude's API doesn't support retrying other messages than the most recent) -
getMessages
<Promise<Message[]>> - The same as callinggetInfo().then(a =\> a.chat_messages)
-
done(response)
response
<object>completion
<string> - The text response from Claude
-
progress(response)
response
<object>completion
<string> - The text response from Claude (if available)
Returned in conversationInstance.getInfo()
's response (chat_messages
key)
sendFeedback(type, reason)
<Promise<{uuid, type, reason, created_at, updated_at}>> - Send feedback about a message
createdAt
<Date> - Created at dateeditedAt
<Date> - Edited at date (Note, there's currently no way to edit messages via the API yet)updatedAt
<Date> - Updated at dateisBot
<Boolean> - Whether the message was send by a human or claudejson
<Object{ uuid, text, sender, index, updated_at, edited_at, chat_feedback, attachments }> - The JSON for the message (this is what's returned from the chat_messages key of a conversation's getInfo fetch request)
type Attachment {
modified_at: string
created_at: string
filetype: string
size: int
}
type MessageStream {
completion: string
stop_reason: string | null
model: string
log_id: string
// "within_limit" or probably "exceeded_limit"
messageLimit: {type: string}
}
type Message {
attachments: Attachment[]
chat_feedback: ??
edited_at: string?
index: int
sender: 'human' | 'assistant'
text: string
created_at: string
updated_at: string
uuid: string
}
The claude-cli
CLI tool is also available:
npm install -g claude-cli
Usage:
claude [options]
Options:
--conversation-id Conversation ID to continue
--json Print response as JSON
--files Comma-separated list of files to attach
--help Show help message
--model Claude model to use (default: claude-2)
--markdown Whether to render markdown in the terminal (default: true)
--key Path to a text file containing the sessionKey cookie value
Examples:
claude --conversation-id fc6d1a1a-8722-476c-8db9-8a871c121ee9
claude --json
claude --files file1.txt,file2.txt
echo "hello world" | claude
Contributions welcome! This library was created by @Explosion-Scratch on GitHub. Please submit PRs to help improve it.# Docs
npm install claude-ai
First, import the library:
const Claude = require("claude-ai");
Initialize a new Claude instance with your session key:
Note
GetsessionKey
from thesessionKey
cookie via the Claude website.
const claude = new Claude({
sessionKey: "YOUR_SESSION_KEY",
});
The main class for interfacing with the Claude API.
const claude = new Claude({
sessionKey: string,
proxy: string | function
})
sessionKey
<string> - Your ClaudesessionKey
cookieproxy
<string | function>- If
proxy
is a string, it will be prepended before the API endpoint, example:https://claude.ai/
- If
proxy
is a function, it will be passed the API route to fetch as well as the fetch options which can then be manipulated before running through fetch.
- If
startConversation(prompt, params)
<Promise<Conversation>> - Starts a new conversation with the given prompt messageprompt
<string> - The initial prompt for the conversationparams
<object> - The parameters to pass to the initialsendMessage
call.
getConversations()
<Promise<Conversation[]>> - Gets recent conversationsclearConversations()
<Promise<Response[]>> - Clear all conversationsuploadFile(file)
<Promise<Attachment>> - Uploads a filefile
<File> - File object to upload
Returned by Claude.startConversation()
.
-
sendMessage(message, options)
<Promise<MessageStream>> - Sends a followup message in the conversationmessage
<string> - The message to sendoptions
<object>timezone
<string> - The timezone for completion (default:"America/New_York"
)attachments
<File[]> - Array of file attachmentsmodel
<string> - The Claude model to use (default:"claude-2"
)done
<(MessageStream) => void> - Callback when completedprogress
<(MessageStream) => void> - Progress callback from the progress, a block of text where each line starts with "data:" then has some JSON.rawResponse
<function> Passed the raw response text
-
getInfo()
<Promise<Conversation>> - Gets the conversation info (includes messages, name, created_at, updated_at, etc) -
delete()
<Promise<Response>> - Delete the conversation -
rename(title)
<Promise<Response>> - Rename a conversation -
retryMessage()
<Promise<MessageStream>> - Retry the last message in the conversation (claude's API doesn't support retrying other messages than the most recent) -
getMessages
<Promise<Message[]>> - The same as callinggetInfo().then(a =\> a.chat_messages)
-
done(response)
response
<object>completion
<string> - The text response from Claude
-
progress(response)
response
<object>completion
<string> - The text response from Claude (if available)
Returned in conversationInstance.getInfo()
's response (chat_messages
key)
sendFeedback(type, reason)
<Promise<{uuid, type, reason, created_at, updated_at}>> - Send feedback about a message
createdAt
<Date> - Created at dateeditedAt
<Date> - Edited at date (Note, there's currently no way to edit messages via the API yet)updatedAt
<Date> - Updated at dateisBot
<Boolean> - Whether the message was send by a human or claudejson
<Object{ uuid, text, sender, index, updated_at, edited_at, chat_feedback, attachments }> - The JSON for the message (this is what's returned from the chat_messages key of a conversation's getInfo fetch request)
type Attachment {
modified_at: string
created_at: string
filetype: string
size: int
}
type MessageStream {
completion: string
stop_reason: string | null
model: string
log_id: string
// "within_limit" or probably "exceeded_limit"
messageLimit: {type: string}
}
type Message {
attachments: Attachment[]
chat_feedback: ??
edited_at: string?
index: int
sender: 'human' | 'assistant'
text: string
created_at: string
updated_at: string
uuid: string
}
The claude-cli
CLI tool is also available:
npm install -g claude-cli
Usage:
claude [options]
Options:
--conversation-id Conversation ID to continue
--json Print response as JSON
--files Comma-separated list of files to attach
--help Show help message
--model Claude model to use (default: claude-2)
--markdown Whether to render markdown in the terminal (default: true)
--key Path to a text file containing the sessionKey cookie value
Examples:
claude --conversation-id fc6d1a1a-8722-476c-8db9-8a871c121ee9
claude --json
claude --files file1.txt,file2.txt
echo "hello world" | claude
Contributions welcome! This library was created by @Explosion-Scratch on GitHub. Please submit PRs to help improve it.