Are you developing Google Sheets functions or Google Workspace add-ons that tap into the power of Google Gemini AI or OpenAI? This tutorial explains how you can use Google Apps Script to verify that the API keys provided by the user are valid and working.

The scripts make an HTTP request to the AI service and check if the response contains a list of available models or engines. There’s no cost associated with this verification process as the API keys are only used to fetch the list of available models and not to perform any actual AI tasks.

Verify Google Gemini API Key

The snippet makes a GET request to the Google Gemini API to fetch the list of available models. If the API key is valid, the response will contain a list of models. If the API key is invalid, the response will contain an error message.

const verifyGeminiApiKey = apiKey => {
  const API_VERSION = "v1";
  const apiUrl = `https://generativelanguage.googleapis.com/${API_VERSION}/models?key=${apiKey}`;
  const response = UrlFetchApp.fetch(apiUrl, {
    method: "GET",
    headers: { "Content-Type": "application/json" },
    muteHttpExceptions: true,
  });
  const { error } = JSON.parse(response.getContentText());
  if (error) {
    throw new Error(error.message);
  }
  return true;
};

This snippet works with Gemini API v1. If you are using Gemini 1.5, you need to update the API_VERSION variable in the script.

Verify OpenAI API Key

The Apps Script snippet makes a GET request to the OpenAI API to fetch the list of available engines. Unlike Gemini API where the key is passed as a query parameter in the URL, OpenAI requires the API key to be passed in the Authorization header.

If the API key is valid, the response will contain a list of engines. If the API key is invalid, the response will contain an error message.

const verifyOpenaiApiKey = apiKey => {
  const apiUrl = `https://api.openai.com/v1/engines`;
  const response = UrlFetchApp.fetch(apiUrl, {
    method: "GET",
    headers: { "Content-Type": "application/json", Authorization: `Bearer ${apiKey}` },
    muteHttpExceptions: true,
  });
  const { error } = JSON.parse(response.getContentText());
  if (error) {
    throw new Error(error.message);
  }
  return true;
};



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *

Author

prakhar@affmantra.com

Related Posts

How to Handle OAuth Permissions in Google Add-ons

Table of Contents 1. How to Check for Required OAuth Scopes 1.1 The “Authorization Catch-22” Problem 1.2 How to Reset the Permissions...

Read out all

How to Recover Permanently Deleted Files and Folders in Google Drive

Table of Contents When you delete any file or folder in your Google Drive, it is moved to the trash folder. The...

Read out all

Simple URL Tricks for Google Drive You Should Know

Table of Contents 1. Google Drive URL Tricks 1.1 Google Drive Web Viewer 1.2 Reader Mode for Google Drive Files 1.3 Embed...

Read out all

How to Extract URLs from HYPERLINK Function in Google Sheets

The HYPERLINK formula of Google Sheets lets you insert hyperlinks into your spreadsheets. The function takes two arguments: The full URL of...

Read out all

Find and Remove Inactive Users in your Google Workspace Domain

Table of Contents 1. Find the inactive users in Google Workspace domain You can use Google Apps Script to find all the...

Read out all

The Best Online Tools To Know Everything About a Website

The Best Online Tools To Know Everything About a Website Source link

Read out all