πŸ‘€ Overview

Linkup can be used with GoogleSheet as a Formula to get contextual information from the internet.

πŸ“¦ Installation

Setting Up Your Environment

  1. Open your Google Sheet.

  2. Go to Extensions > Apps Script.

  1. Copy and paste the script into the Apps Script editor.
/**
 * Linkup API configuration
 */
const API_ENDPOINT = 'https://api.linkup.so/v1/search';

/**
 * Creates the menu item
 */
function onOpen() {
  const ui = SpreadsheetApp.getUi();
  ui.createMenu('Linkup')
    .addItem('Settings', 'settings')
    .addToUi();
}

/**
 * Shows the settings dialog to configure the API key
 */
function settings() {
  const ui = SpreadsheetApp.getUi();
  const response = ui.prompt(
    'Settings',
    'Please enter your Linkup API Key (visit https://app.linkup.so/sign-up to get your free API key)',
    ui.ButtonSet.OK_CANCEL
  );

  if (response.getSelectedButton() === ui.Button.OK) {
    const apiKey = response.getResponseText();
    if (apiKey) {
      PropertiesService.getUserProperties()
        .setProperty('LINKUP_API_KEY', apiKey);
    } else {
      ui.alert('Please enter a valid Linkup API key');
    }
  }
}

/**
 * Returns the Linkup API key
 */
function getApiKey() {
  return PropertiesService.getUserProperties().getProperty('LINKUP_API_KEY');
}

/**
 * Returns the active cell
 */
function getActiveCell() {
  return SpreadsheetApp.getActiveRange().getA1Notation();
}

/**
 * Returns cells cache
 */
function getCache() {
  return PropertiesService.getScriptProperties().getProperty('LINKUP_DATA')
      ? JSON.parse(PropertiesService.getScriptProperties().getProperty('LINKUP_DATA')) : {}
}

/**
 * Overrides cells cache
 */
function saveCache(cache) {
  PropertiesService.getScriptProperties()
    .setProperty('LINKUP_DATA', JSON.stringify(cache));
}

/**
 * Clears the cache of the active cell
 */
function deleteCellCache() {
  const cache = getCache();
  const activeCell = getActiveCell();

  if (cache[activeCell]) {
    delete cache[activeCell];
    saveCache(cache);
  }
}

/**
 * Triggered on cell edit
 */
function onEdit(e) {
  if (e.oldValue) {
    deleteCellCache();
  }
}

function askLinkup(query, include_sources) {
  if (!query) return 'Please provide a search query';

  const activeCell = getActiveCell();
  const apiKey = getApiKey();
  const cache = getCache();
  console.log("api kEY=", apiKey)
  console.log("cache =", cache)

  // Check if this search has been cached
  const existingEntry = cache[activeCell];
  if (existingEntry
      && existingEntry.query === query
      && existingEntry.include_sources === include_sources) {
    return existingEntry.response;
  }

  const options = {
    method: 'post',
    headers: {
      Authorization: `Bearer ${apiKey}`,
      'Content-Type': 'application/json',
    },
    payload: JSON.stringify({
      q: query,
      depth: 'standard',
      outputType: 'sourcedAnswer',
    }),
    muteHttpExceptions: true,
  };

  try{
    const response = JSON.parse(
      UrlFetchApp.fetch(API_ENDPOINT, options).getContentText()
    );
    if (response.answer) {
      cache[activeCell] = { query, response };
      saveCache(cache);
    }
    return response;
  } catch (error) {
    throw new Error(`Failed to process request: ${error.message}`)
  }
}

/**
 * Searches the Linkup API with the given query and returns the answer.
 * @param {string} query The search query.
 * @customfunction
 */
function LINKUP(query, includeSources=false) {
  const response = askLinkup(query, includeSources);
  const answer = response.answer || 'No answer found';

  if (!includeSources) {
    return answer;
  }

  if (response.sources && response.sources.length > 0) {
    return `${answer}\nSources: ${response.sources.map(({ url }) => url).join(':')}`
  } else {
    return `${answer}\nNo sources available`;
  }
}
  1. Save the script and refresh your Google Sheet.

βš™οΈ ️Configure you API Key

  1. Get an API Key:

Get your API key

Create a Linkup account for free to get your API key.

  1. In your Google Sheet, Linkup > Settings (the system may ask you confirmation to run a script)

  1. Enter your API Key and click Ok

πŸ€– Usage

Use Linkup to answer your questions

  1. In a cell type:
=LINKUP("your query")

You can also use a reference to a different cell:

=LINKUP(B14)
  1. Press enter βœ…

List sources in addition to the answer

  1. In a cell type:
=LINKUP("your query", TRUE)

You can also use a reference to a different cell:

=LINKUP(B14, TRUE)
  1. Press enter βœ…

Facing issues? Reach out to our engineering team at support@linkup.so