Skip to main content
This example demonstrates how to integrate Composio tools with OpenAI’s Chat Completions API to fetch data from HackerNews.

Overview

In this example, you’ll learn how to:
  • Initialize Composio with OpenAI
  • Fetch and use tools from Composio
  • Handle tool calls from OpenAI responses
  • Execute tools and process results

Prerequisites

1

Install dependencies

npm install @composio/core openai
2

Set up environment variables

Create a .env file with your API keys:
COMPOSIO_API_KEY=your_composio_api_key
OPENAI_API_KEY=your_openai_api_key

Complete Example

import { Composio } from '@composio/core';
import { OpenAI } from 'openai';

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});

/**
 * Initialize Composio
 * OpenAI Provider is automatically installed and initialized
 */
const composio = new Composio({
  apiKey: process.env.COMPOSIO_API_KEY,
});

/**
 * Get the tools
 * This tool is automatically typed and wrapped with the OpenAI Provider
 */
const tools = await composio.tools.get('default', 'HACKERNEWS_GET_USER');

/**
 * Define a task for the assistant based on the tools in hand
 */
const task = "Fetch the details of the user 'haxzie'";

/**
 * Define the messages for the assistant
 */
const messages: OpenAI.ChatCompletionMessageParam[] = [
  { role: 'system', content: 'You are a helpful assistant that can help with tasks.' },
  { role: 'user', content: task },
];

/**
 * Create a chat completion
 */
const response = await openai.chat.completions.create({
  model: 'gpt-4o-mini',
  messages,
  tools: tools,
  tool_choice: 'auto',
});

/**
 * If the assistant has tool calls, execute them and log the result
 */
if (
  response.choices[0].message.tool_calls &&
  response.choices[0].message.tool_calls[0].type === 'function'
) {
  console.log(JSON.stringify(response, null, 2));
  const toolCall = response.choices[0].message.tool_calls[0];
  if (toolCall.type === 'function') {
    console.log(`✅ Calling tool ${response.choices[0].message.tool_calls[0].function.name}`);
  }
  const result = await composio.provider.handleToolCalls('default', response);
  console.log(result);
}

How It Works

1

Initialize OpenAI and Composio

First, we create instances of both the OpenAI client and Composio SDK. Composio automatically detects and initializes the OpenAI provider.
2

Fetch Tools

We fetch the HACKERNEWS_GET_USER tool from Composio. The tool is automatically formatted to work with OpenAI’s function calling format.
3

Create Chat Completion

We send a message to OpenAI along with the available tools. OpenAI decides whether to use the tool based on the user’s request.
4

Handle Tool Calls

If OpenAI decides to call a tool, we use Composio’s handleToolCalls method to execute the tool and get the result.

Expected Output

When you run this example, you’ll see:
{
  "id": "chatcmpl-...",
  "object": "chat.completion",
  "created": 1234567890,
  "model": "gpt-4o-mini",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "tool_calls": [
          {
            "id": "call_...",
            "type": "function",
            "function": {
              "name": "HACKERNEWS_GET_USER",
              "arguments": "{\"username\":\"haxzie\"}"
            }
          }
        ]
      },
      "finish_reason": "tool_calls"
    }
  ]
}
✅ Calling tool HACKERNEWS_GET_USER
{
  "data": {
    "id": "haxzie",
    "created": 1234567890,
    "karma": 1234,
    "about": "..."
  }
}

Next Steps

Anthropic Integration

Learn how to use Composio with Anthropic Claude

Custom Tools

Create your own custom tools with Composio

Build docs developers (and LLMs) love