Skip to main content
This comprehensive example demonstrates how to use the Incredible TypeScript SDK to orchestrate Google Sheets integration with function calling. The demo shows OAuth flow, integration discovery, and automated spreadsheet creation using tool-calling patterns.

Key Features

  • OAuth Integration: Complete Google Sheets OAuth flow with callback handling
  • Function Calling: Uses Anthropic’s tool-calling to orchestrate multiple integration operations
  • Integration Discovery: Lists and inspects available integrations and their features
  • Automated Workflow: Creates spreadsheets and populates them with integration data

Prerequisites

  • Node.js 18+
  • Incredible API key
  • ngrok (or HTTPS tunnel) for OAuth callback
  • Google account with Sheets access

Setup

  1. Install dependencies:
npm install @incredible-ai/sdk @anthropic-ai/sdk dotenv tsx typescript
  1. Create environment file:
cp .env.example .env
  1. Configure your environment:
INCREDIBLE_API_KEY=ik_your_incredible_api_key
INCREDIBLE_BASE_URL=https://api.incredible.one
  1. Start ngrok for OAuth callback:
ngrok http 3001
  1. Update the callback URL in your code with the ngrok HTTPS URL.

Code Example

import { IncredibleClient } from "@incredible-ai/sdk";
import type { MessageParam, Tool, ToolUseBlock } from "@anthropic-ai/sdk";

const API_KEY = "ik_your_incredible_api_key";
const USER_ID = "integration-demo-user";
const INTEGRATION_ID = "googlesheets";
const CALLBACK_BASE = "https://your-ngrok-url.ngrok-free.app";
const CALLBACK_PATH = "/oauth/google-sheets/callback";

async function main() {
  const client = new IncredibleClient({ apiKey: API_KEY });
  
  // Phase 1: Discover integrations
  const integrations = await client.integrations.list();
  console.log("Available integrations:", integrations);

  // Phase 2: OAuth flow for Google Sheets
  await ensureOAuthAuthorized(client, buildCallbackUrl());

  // Phase 3: Build tools for function calling
  const tools = buildGoogleSheetsTools(client);
  const messages = createInitialMessages();

  // Phase 4: Run agent conversation with tool calling
  await runAgentConversation(client, tools, messages);
}

// Tool definitions for function calling
function buildGoogleSheetsTools(client: IncredibleClient) {
  return [
    {
      definition: {
        name: "list_incredible_integrations",
        description: "Return the first ten Incredible integrations",
        input_schema: { type: "object", properties: {} }
      },
      run: async () => {
        const all = await client.integrations.list();
        return all.slice(0, 10);
      }
    },
    {
      definition: {
        name: "create_google_sheet",
        description: "Create a new Google Sheet",
        input_schema: {
          type: "object",
          properties: {
            title: { type: "string" }
          },
          required: ["title"]
        }
      },
      run: async (args) => {
        return client.integrations.execute(INTEGRATION_ID, {
          user_id: USER_ID,
          feature_name: "GOOGLESHEETS_CREATE_GOOGLE_SHEET1",
          inputs: { title: args.title }
        });
      }
    },
    {
      definition: {
        name: "write_google_sheets_from_json",
        description: "Write data to Google Sheets from JSON",
        input_schema: {
          type: "object",
          properties: {
            spreadsheet_title: { type: "string" },
            sheet_title: { type: "string" },
            rows_json: { type: "string" }
          },
          required: ["spreadsheet_title", "sheet_title", "rows_json"]
        }
      },
      run: async (args) => {
        return client.integrations.execute(INTEGRATION_ID, {
          user_id: USER_ID,
          feature_name: "GOOGLESHEETS_SHEET_FROM_JSON",
          inputs: {
            title: args.spreadsheet_title,
            sheet_name: args.sheet_title,
            sheet_json: JSON.parse(args.rows_json)
          }
        });
      }
    }
  ];
}

// OAuth flow implementation
async function ensureOAuthAuthorized(client: IncredibleClient, callbackUrl: string) {
  const connectResponse = await client.integrations.connect(INTEGRATION_ID, {
    callback_url: callbackUrl,
    user_id: USER_ID
  });

  if (connectResponse?.redirect_url) {
    console.log(`Visit this URL to authorize: ${connectResponse.redirect_url}`);
    // Wait for user authorization...
  }
}

// Agent conversation with tool calling
async function runAgentConversation(client: IncredibleClient, tools: RegisteredTool[], messages: MessageParam[]) {
  for (;;) {
    const response = await client.messages.create({
      model: "small-1",
      max_tokens: 1024,
      messages,
      tools: tools.map(tool => tool.definition)
    });

    messages.push({ role: response.role, content: response.content });

    if (response.stop_reason !== "tool_use") {
      return;
    }

    const toolUses = extractToolUses(response);
    for (const toolUse of toolUses) {
      await invokeTool(tools, toolUse, messages);
    }
  }
}

Running the Demo

npx tsx src/incredible_integration.ts
The script will:
  1. List available integrations
  2. Prompt for Google Sheets OAuth authorization
  3. Create a spreadsheet with integration data
  4. Populate it using function calling
  5. Return the Google Sheets URL

Key Concepts

OAuth Flow

The demo implements a complete OAuth flow with callback handling, allowing secure authorization with Google Sheets.

Function Calling

Uses Anthropic’s tool-calling capabilities to orchestrate multiple integration operations in sequence, allowing the AI to decide which tools to use and when.

Integration Discovery

Demonstrates how to discover available integrations and their features programmatically.

Automated Workflows

Shows how to combine multiple integration operations into automated workflows using AI orchestration.
View complete source code on GitHub → incredible_integration_demo