Skip to main content
Glama

api_post

Send data to an API endpoint using a POST request. Specify the URL, JSON body, and headers to interact with web services and automate API-based tasks effectively.

Instructions

Perform a POST request to an API endpoint

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
dataYesRequest body data (JSON string)
headersNoRequest headers
urlYesAPI endpoint URL

Implementation Reference

  • The handler function that executes the api_post tool: sends POST request with provided data and headers, retrieves response, and returns formatted result or error.
    async function handleApiPost(client: APIRequestContext, args: any): Promise<{ toolResult: CallToolResult }> {
      try {
        const options = {
          data: args.data,
          headers: args.headers || { 'Content-Type': 'application/json' }
        };
    
        const response = await client.post(args.url, options);
        const responseData = await getResponseData(response);
    
        return {
          toolResult: {
            content: [
              {
                type: "text",
                text: `POST ${args.url} - Status: ${response.status()}`,
              },
              ...responseData
            ],
            isError: false,
          },
        };
      } catch (error) {
        return {
          toolResult: {
            content: [{
              type: "text",
              text: `POST request failed: ${(error as Error).message}`,
            }],
            isError: true,
          },
        };
      }
  • The Tool schema definition for api_post, including name, description, and inputSchema with required url and data.
    {
      name: "api_post",
      description: "Perform a POST request to an API endpoint",
      inputSchema: {
        type: "object",
        properties: {
          url: { type: "string", description: "API endpoint URL" },
          data: { type: "string", description: "Request body data (JSON string)" },
          headers: { 
            type: "object", 
            description: "Request headers",
            additionalProperties: { type: "string" }
          }
        },
        required: ["url", "data"]
      }
    },
  • Switch case in executeToolCall that registers and dispatches the api_post tool call to its handler.
    case "api_post":
      return await handleApiPost(apiClient!, args);
  • API_TOOLS constant listing api_post among API tools, used to identify and initialize API client in executor.
    export const API_TOOLS = [
      "api_get",
      "api_post",
      "api_put",
      "api_patch",
      "api_delete"
    ];
  • Helper function getResponseData used by api_post handler to process and format API response body.
    async function getResponseData(response: any): Promise<TextContent[]> {
      const contentType = response.headers()['content-type'] || '';
      let responseText: string;
      if (contentType.includes('application/json')) {
        try {
          const json = await response.json();
          responseText = JSON.stringify(json, null, 2);
        } catch (e) {
          responseText = await response.text();
        }
      } else {
        responseText = await response.text();
      }
      return [{
        type: "text",
        text: `Response body:\n${responseText}`,
      } as TextContent];
    }
Install Server

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/imprvhub/mcp-browser-agent'

If you have feedback or need assistance with the MCP directory API, please join our Discord server