Using Anthropic Claude AI with Drafts

For integrations with other AI platforms, see the Artificial Intelligence article in the User Guide.

Drafts provides scripting wrappers to simplify interaction with Anthropic’s Claude AI through its Claude API.

This article covers the setup required to use such actions, which integrate with Claude, and provides useful examples of actions for common use cases, like text manipulation and prompts.

Setting Up Claude API Access

In order to use any of these integrations, you will need a Anthropic account and an API key to use with Drafts.

The Anthropic API requires you configure your account and billing information. Information on costs at the Anthropic side. Generally speaking, occasional usage in Drafts will not be costly, and it is billed per usage and not based on any monthly fees.

To get an API key, visit the Anthropic Dashboard. Use the “Get API key” link, and copy the API key generated for use in Drafts. You will be prompted to enter the key the first time you use one of the example actions below.

Drafts will remember the API key in its Credentials system, so you will only need to complete this step once. If you need to change the API key used, you can forget it in the Credentials pane in Drafts settings, and the next time you use a Claude action, you will be prompted to enter a new key.

Example Actions

Below are a few example use-case actions that are meant as a starting point to demonstrate some ways Google Gemini integration can be used in Drafts:

  • Ask Claude: This action will ask you to enter a text prompt for Gemini and insert the result in the current draft.
  • Claude: Converse: Mimics a chat bot interface, so allow an on-going conversation with Claude.
  • Claude: Translate Selection: Take the selected text in the editor, and ask Claude to translate it into another language. You will be prompted to select from a list of languages.
  • Claude: Modify Selection: Select text in a draft, then run this action, and you will be prompted for an instruction to transform the text. Drafts will package that up in a prompt and replace the selected text with the result. You can do simple things like “uppercase” but also combine commands for things like “uppercase and insert a :tada: emoji between each word.”

Scripting with AnthropicAI

If you wish to build your own more advanced integrations, the AnthropicAI script object is your starting point.

This object provides a convenience wrapper for making API calls to the Anthropic AI API. When making requests with this object, Drafts will take care of requesting and storing a user’s API key, providing the appropriate authentication headers, and parsing results into Javascript objects.

The object provides several additional simple request functions, like quickPrompt that abstract details about the API for simple use cases, but also the request function to build more detailed requests with all the API options. Refer to Anthropic’s API documentation on request parameters and return values.

The below action is meant as a starting point and demonstrates the use of the request function. Get more examples in the scripting reference:

Troubleshooting

If you run into issues running these actions, be sure to check the Action Log for detailed error messages. Common problems include rate limits or improperly configured API keys.

Conclusion

If you create new and interesting actions with this functionality, we hope you’ll share them in the directory and forums!

2 Likes

Thank you very much for Claude access! I was able to use Ask Claude, but when I try to use Converse, I get the following error:

AnthropicAI: 400, {“type”:“error”,“error”:{“type”:“invalid_request_error”,“message”:“messages: roles must alternate between "user" and "assistant", but found multiple "assistant" roles in a row”}}
ERROR: 400
{“type”:“error”,“error”:{“type”:“invalid_request_error”,“message”:“messages: roles must alternate between "user" and "assistant", but found multiple "assistant" roles in a row”}}
undefined
undefined
Script step completed.

What I am doing wrong?

Update: I was trying to converse at the end of a reply coming from Ask. Stupid of me!

I updated the script to merge consecutive assistant or user lines before sending to Claude, so if you re-install the action it should be more resilient. I didn’t realize Claude would not merge those messages at their end.

1 Like

Hello all! I am having an issue with using AnthropicAI on the Mac app. It works perfectly fine on mobile, but I get an error on Mac. Forgive me in advance, I am not familiar with coding at all.

Strange, I just got the same error on iOS.
I have installed the scripts and the credentials on Mac. They work nicely and the Draft on iOS has synched them spontaneously (very nice!) but when I “Ask Claude” I got this error.
I will try again.

That original is what I received for “Ask Claude”, below is what I get for “Claude: Converse”.

Did you install the v44 update?

1 Like

I have now. And sure enough, it works. Sorry about that. I thought I had updated everything.

1 Like

Sorry for not being able to check it. Now updated and it works like a charm :smiling_face:

Hello, another question about the Claude model used by Draft. It seems to be the first element of the models list. Is it Haiku, then? I do not know the list returned for models constant.
Thank you!

Yes, it returns the three available models from simplest/cheapest to most sophisticated/expensive. Which, at least currently, is the current versions of haiku, sonnet, and open.

Thank you very much! By changing the index, we could change the model then. Good to know.

You can just hard-code a model name as well. Just be sure it matches one of the available models from the docs. The knownModels function is just a shortcut for the common, recommended versions.

1 Like