Make HTTP Requests to External APIs
RequestsToolSpec enables LlamaIndex agents to make HTTP GET, POST, and PATCH requests with domain-specific header authentication for secure API interactions.
Why it matters
Enable AI agents to interact with external web services by making HTTP requests. This tool allows agents to fetch data from or send data to APIs, expanding their capabilities beyond internal knowledge.
Outcomes
What it gets done
Perform GET requests to retrieve data from URLs.
Execute POST requests to send data to APIs.
Handle PATCH requests for updating resources.
Securely manage API credentials for specific hostnames.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/li-tool-tools-requests | bash Overview
Requests Tool
RequestsToolSpec is a LlamaIndex tool that gives agents the ability to make HTTP requests (GET, POST, PATCH) to external APIs. It enforces security by requiring explicit hostname-to-header mappings, ensuring authentication credentials are only sent to authorized domains. The tool exposes three request methods and can be combined with OpenAPIToolSpec for schema-driven API interactions. Use this tool when building LlamaIndex agents that need to fetch data from REST APIs, submit forms, or update resources via HTTP. It's particularly useful when integrating with third-party services or internal microservices that expose HTTP endpoints, and when security requirements demand explicit control over which credentials are sent to which domains.
What it does
RequestsToolSpec equips LlamaIndex agents with the ability to make HTTP requests (GET, POST, PATCH) to external APIs. It provides a security-first approach by requiring explicit hostname-to-header mappings, ensuring credentials are only sent to authorized domains. The tool can be combined with OpenAPIToolSpec to interface with OpenAPI servers, enabling agents to interact with RESTful services.
When to use - and when NOT to
Use RequestsToolSpec when your agent needs to fetch data from REST APIs, submit form data, or update resources via HTTP methods. Combine it with OpenAPIToolSpec when working with OpenAPI-documented endpoints.
Avoid it when your security model cannot accommodate pre-configured domain headers, as the tool requires upfront hostname-to-credential mapping rather than dynamic authentication.
Inputs and outputs
You provide a domain_headers dictionary mapping hostnames to their required HTTP headers (authorization tokens, content types, custom headers). The tool exposes three methods to your agent: get_request for retrieving data, post_request for creating resources, and patch_request for updating resources. Each method accepts a URL and returns the HTTP response, which the agent can parse and act upon.
Integrations
RequestsToolSpec integrates with OpenAPIToolSpec to interface with OpenAPI servers, enabling schema-driven API interactions. It works within the LlamaIndex agent framework, specifically with FunctionAgent and compatible LLM providers like OpenAI models. The tool is designed as a ToolSpec that converts to a tool list consumable by LlamaIndex agents.
Who it's for
This tool serves developers building LlamaIndex agents that need to interact with external HTTP APIs.
from llama_index.tools.requests import RequestsToolSpec
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI
domain_headers = {
"api.openai.com": {
"Authorization": "Bearer sk-your-key",
"Content-Type": "application/json",
}
}
tool_spec = RequestsToolSpec(domain_headers=domain_headers)
agent = FunctionAgent(
tools=tool_spec.to_tool_list(),
llm=OpenAI(model="gpt-4.1"),
)
print(await agent.run("<query>"))
Source README
Requests Tool
This tool provides the agent the ability to make HTTP requests. It can be combined with the OpenAPIToolSpec to interface with an OpenAPI server.
For security reasons, you must specify the hostname for the headers that you wish to provide. See here for an example
Usage
This tool has more extensive example usage documented in a Jupyter notebook here
Here's an example usage of the RequestsToolSpec.
from llama_index.tools.requests import RequestsToolSpec
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI
domain_headers = {
"api.openai.com": {
"Authorization": "Bearer sk-your-key",
"Content-Type": "application/json",
}
}
tool_spec = RequestsToolSpec(domain_headers=domain_headers)
agent = FunctionAgent(
tools=tool_spec.to_tool_list(),
llm=OpenAI(model="gpt-4.1"),
)
print(await agent.run("<query>"))
get_request: Performs a get request against the URLpost_request: Performs a post request against the URLpatch_request: Performs a patch request against the URL
This loader is designed to be used as a way to load data as a Tool in a Agent.
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.