Using Google Gemini 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 Google Gemini through the Google Generative Language API.

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

Setting Up Google Gemini API Access

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

The Gemini API can be used free of charge with usage limits. If you exceed those limits, you will need to set up a paid account with Google. Information on costs at their pricing page. 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 Google AI Studio. Use the “Create 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 Gemini 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 Gemini: This action will ask you to enter a text prompt for Gemini and insert the result in the current draft.
  • Gemini: Translate Selection: Take the selected text in the editor, and ask Gemini to translate it into another language. You will be prompted to select from a list of languages.
  • Gemini: 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 GoogleAI

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

This object provides a convenience wrapper for making API calls to the Google 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 Google 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!

5 Likes

I use Gemini to summarize YouTube videos.

When using the action to attempt to send a link to a video to summarize, it responds with the summary of a different video. Running the action more than once will return the summary of a different video, but never the correct one.

Any idea what’s going on? Thanks.

Generally speaking, that is not something LLMs like Gemini are good at, because they cannot get live content from the web and summarize it. These actions use the Gemini APIs which make direct requests from Google’s Gemini APIs, which make request of the Gemini LLM (sorry for the redundancy).

This is not the same thing you get when using gemini.google.com. It employs a variety of extensions and other mechanisms to utilize sources other than Gemini’s LLM when answering. For example, you’ll notice if you put in “summarize [youtube URL]” the response you get indicates that it made a request to YouTube for information.

Hopefully that helps clarify.

Makes perfect sense, thanks :+1:

Hi. Need v1beta API version(now v1), so that you can use latest models like 2.0-flash.

Refer Giải thích về các phiên bản API  |  Gemini API  |  Google AI for Developers. :pray:


Guess I’ve got it. But I don’t know what is the difference without Class GoogleAI.

let http = HTTP.create(); // 假设你有一个 HTTP 工具类

let response = http.request({
  "url": "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent?key=YOU_KEY",
  "method": "POST",
  "data": {
    contents: [{
      parts: [{
        text: "你是一个智能助手"
      }]
    }]
  },
  "headers": {
    "Content-Type": "application/json"
  }
});

if (response.success) {
  var text = response.responseData.candidates[0].content.parts[0].text;
  var data = response.responseData;
  var thisDraft = Draft.find("43422D83-F3E5-41E3-A183-8B29353CDBE0");
  thisDraft.append(text);
  thisDraft.update();
  console.log(thisDraft.content);
}
else {
  console.log(response.statusCode);
  console.log(response.error);
}
1 Like

The GoogleAI class is just a convenience wrapper and takes care of remembering an API key for you, but you can always use HTTP if it does not do something you need. I should expose the API version as a property, however, now that there are multiple options, so it could be overridden. Will do that.

1 Like

Found my way to this thread because Gemini has reportedly become more capable in recent days. Might be worth noting that the default model in the Ask Gemini might need to be updated on first use, for anyone installing it now (time and AI move quickly, and this was first posted in 2024.

If you get a little confused (like I did) check the models list here: Gemini models  |  Gemini API  |  Google AI for Developers and copy the appropriate model into the script, being sure to retain the “model/” root ahead of each model in your list.

Thanks for the reminder. I update the action to include a couple of the newer model options.

1 Like

I can’t get this to work. Trying to use gemini-3-pro-preview and keep getting this error:

GoogleAI: 404, Invalid path. Check that your request is for a valid endpoint and model in the Google AI API.
Gemini returned an empty or invalid response.
undefined
Script step completed.

My script:

// get your input selected text, and store range for laterconst selection = editor.getSelectedText()const [st, len] = editor.getSelectedRange()

// setup model name to use// FIX: Explicitly specify the model path as requested. This format is needed// for Drafts to send the request correctly and avoid the 404 error.const GeminiModel = “models/gemini-3-pro-previewt”

// Prompts let f = () => {let instruction = `Revise the following text so it is concise, clear, and informative, suitable for inclusion in a psychological report.

Tone and Style:•	Semi-formal and professional•	Follow Canadian English spelling and conventions

Requirements:•	Preserve the abbreviation “MVA” exactly as written•	Eliminate redundancy and use precise language•	Prioritise readability and clarity for a professional audience

Output:Provide the revised text only. Do not include explanations or commentary.

Input:`

// build prompt with instruction and selection
let chatPrompt = `${instruction} "${selection}"`

// Instantiate the AI object
let ai = new GoogleAI() 

// FIX: Use quickPrompt() and pass the model name as the second argument 
// to ensure "gemini-3-pro-preview" is used.
let answer = ai.quickPrompt(chatPrompt, GeminiModel)

// if we got no reply, cancel
if (!answer || answer.length == 0) {
	// Log a failure message if no answer is received
	console.log("Gemini returned an empty or invalid response.")
	return false
}

editor.setSelectedText(answer)
editor.setSelectedRange(st, answer.length)
return true

}

if (!f()) {// Only call context.fail() if the function returns falsecontext.fail() }

That script seems jumbled and didn’t come through quite right. Could you share your action itself? You can post it to the Directory as an unlisted action and share the link here.

Also, just to start, are you sure the new model is available to your API key? You can run this example to get a list of models available for the credentials you are currently using:

@agiletortoise Please take a look at this script: Rewrite Gemini | Drafts Directory

It was constructed using Gemini based on Drafts and the Gemini API documentation. Please let me know if it looks good to you regardless of of the model name.

As for the model, it says that gemini-3-pro-preview API is available as of November 18, 2025

However, the models avialble to me via Drafts action that you posted are:

models/gemini-2.5-flash
models/gemini-2.5-pro
models/gemini-2.0-flash
models/gemini-2.0-flash-001
models/gemini-2.0-flash-lite-001
models/gemini-2.0-flash-lite
models/gemini-2.5-flash-lite
models/embedding-001
models/text-embedding-004

The script looks OK…and works fine if I change the GeminiModel variable to be a valid model available to my API key.

If the gemini-3-pro-preview option is not coming back in the model list, then your API does not have it.

I asked Gemini why it might not be available - and they said it’s limited to paid accounts, and may be in a gradual rollout phase. You might ask the same of Gemini and make sure your API key matches the details it provides.

I asked Gemini to troubleshoot and here is what it said

If Option 1 still gives you an error, it is because the built-in GoogleAI() object in Drafts might be targeting the v1 (Stable) API endpoint, but gemini-3-pro-preview is only available on the v1beta endpoint.

In this case, you must use the HTTP object instead of the GoogleAI helper to force the correct API version.

Here is your full, corrected script using the raw HTTP method:

I inserted my API key directly into the script. The above script does work with gemini-3-pro-preview. So it has to do with how Drafts app makes the call.

You can set the API version on the GoogleAI object. See docs: