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

2

Set up environment variables

Create a .env file with your API keys:

Complete Example

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:

Next Steps

Anthropic Integration

Learn how to use Composio with Anthropic Claude

Custom Tools

Create your own custom tools with Composio