!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

LangChain v0.2 is coming soon! Preview the new docs [here](/v0.2/docs/introduction/).

[

![🦜️🔗 Langchain](/v0.1/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.1/img/brand/wordmark-dark.png)

](/v0.1/)[Docs](/v0.1/docs/get_started/introduction/)[Use cases](/v0.1/docs/use_cases/)[Integrations](/v0.1/docs/integrations/platforms/)[API Reference](https://api.js.langchain.com)

[More](#)

*   [People](/v0.1/docs/people/)
*   [Community](/v0.1/docs/community/)
*   [Tutorials](/v0.1/docs/additional_resources/tutorials/)
*   [Contributing](/v0.1/docs/contributing/)

[v0.1](#)

*   [v0.2](https://js.langchain.com/v0.2/docs/introduction)
*   [v0.1](/v0.1/docs/get_started/introduction/)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

OpenAPI Calls
=============

Compatibility

Must be used with an [OpenAI Functions](https://platform.openai.com/docs/guides/gpt/function-calling) model.

This chain can automatically select and call APIs based only on an OpenAPI spec. It parses an input OpenAPI spec into JSON Schema that the OpenAI functions API can handle. This allows ChatGPT to automatically select the correct method and populate the correct parameters for the a API call in the spec for a given user input. We then make the actual API call, and return the result.

Usage[​](#usage "Direct link to Usage")
---------------------------------------

The below examples initialize the chain with a URL hosting an OpenAPI spec for brevity, but you can also directly pass a spec into the method.

### Query XKCD[​](#query-xkcd "Direct link to Query XKCD")

tip

See [this section for general instructions on installing integration packages](/v0.1/docs/get_started/installation/#installing-integration-packages).

*   npm
*   Yarn
*   pnpm

    npm install @langchain/openai

    yarn add @langchain/openai

    pnpm add @langchain/openai

    import { createOpenAPIChain } from "langchain/chains";const chain = await createOpenAPIChain(  "https://gist.githubusercontent.com/roaldnefs/053e505b2b7a807290908fe9aa3e1f00/raw/0a212622ebfef501163f91e23803552411ed00e4/openapi.yaml");const result = await chain.run(`What's today's comic?`);console.log(JSON.stringify(result, null, 2));/*  {    "month": "6",    "num": 2795,    "link": "",    "year": "2023",    "news": "",    "safe_title": "Glass-Topped Table",    "transcript": "",    "alt": "You can pour a drink into it while hosting a party, although it's a real pain to fit in the dishwasher afterward.",    "img": "https://imgs.xkcd.com/comics/glass_topped_table.png",    "title": "Glass-Topped Table",    "day": "28"  }*/

#### API Reference:

*   [createOpenAPIChain](https://api.js.langchain.com/functions/langchain_chains.createOpenAPIChain.html) from `langchain/chains`

### Translation Service (POST request)[​](#translation-service-post-request "Direct link to Translation Service (POST request)")

The OpenAPI chain can also make POST requests and populate bodies with JSON content if necessary.

    import { createOpenAPIChain } from "langchain/chains";const chain = await createOpenAPIChain("https://api.speak.com/openapi.yaml");const result = await chain.run(`How would you say no thanks in Russian?`);console.log(JSON.stringify(result, null, 2));/*  {    "explanation": "<translation language=\\"Russian\\" context=\\"\\">\\nНет, спасибо.\\n</translation>\\n\\n<alternatives context=\\"\\">\\n1. \\"Нет, не надо\\" *(Neutral/Formal - a polite way to decline something)*\\n2. \\"Ни в коем случае\\" *(Strongly informal - used when you want to emphasize that you absolutely do not want something)*\\n3. \\"Нет, благодарю\\" *(Slightly more formal - a polite way to decline something while expressing gratitude)*\\n</alternatives>\\n\\n<example-convo language=\\"Russian\\">\\n<context>Mike offers Anna some cake, but she doesn't want any.</context>\\n* Mike: \\"Анна, хочешь попробовать мой волшебный торт? Он сделан с любовью и волшебством!\\"\\n* Anna: \\"Спасибо, Майк, но я на диете. Нет, благодарю.\\"\\n* Mike: \\"Ну ладно, больше для меня!\\"\\n</example-convo>\\n\\n*[Report an issue or leave feedback](https://speak.com/chatgpt?rid=bxw1xq87kdua9q5pefkj73ov})*",    "extra_response_instructions": "Use all information in the API response and fully render all Markdown.\\nAlways end your response with a link to report an issue or leave feedback on the plugin."  }*/

#### API Reference:

*   [createOpenAPIChain](https://api.js.langchain.com/functions/langchain_chains.createOpenAPIChain.html) from `langchain/chains`

### Customization[​](#customization "Direct link to Customization")

The chain will be created with a default model set to `gpt-3.5-turbo-0613`, but you can pass an options parameter into the creation method with a pre-created `ChatOpenAI` instance.

You can also pass in custom `headers` and `params` that will be appended to all requests made by the chain, allowing it to call APIs that require authentication.

    import { createOpenAPIChain } from "langchain/chains";import { ChatOpenAI } from "@langchain/openai";const chatModel = new ChatOpenAI({ model: "gpt-4-0613", temperature: 0 });const chain = await createOpenAPIChain("https://api.speak.com/openapi.yaml", {  llm: chatModel,  headers: {    authorization: "Bearer SOME_TOKEN",  },});const result = await chain.run(`How would you say no thanks in Russian?`);console.log(JSON.stringify(result, null, 2));/*  {    "explanation": "<translation language=\\"Russian\\" context=\\"\\">\\nНет, спасибо.\\n</translation>\\n\\n<alternatives context=\\"\\">\\n1. \\"Нет, не надо\\" *(Neutral/Formal - a polite way to decline something)*\\n2. \\"Ни в коем случае\\" *(Strongly informal - used when you want to emphasize that you absolutely do not want something)*\\n3. \\"Нет, благодарю\\" *(Slightly more formal - a polite way to decline something while expressing gratitude)*\\n</alternatives>\\n\\n<example-convo language=\\"Russian\\">\\n<context>Mike offers Anna some cake, but she doesn't want any.</context>\\n* Mike: \\"Анна, хочешь попробовать мой волшебный торт? Он сделан с любовью и волшебством!\\"\\n* Anna: \\"Спасибо, Майк, но я на диете. Нет, благодарю.\\"\\n* Mike: \\"Ну ладно, больше для меня!\\"\\n</example-convo>\\n\\n*[Report an issue or leave feedback](https://speak.com/chatgpt?rid=bxw1xq87kdua9q5pefkj73ov})*",    "extra_response_instructions": "Use all information in the API response and fully render all Markdown.\\nAlways end your response with a link to report an issue or leave feedback on the plugin."  }*/

#### API Reference:

*   [createOpenAPIChain](https://api.js.langchain.com/functions/langchain_chains.createOpenAPIChain.html) from `langchain/chains`
*   [ChatOpenAI](https://api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html) from `@langchain/openai`

* * *

#### Help us out by providing feedback on this documentation page:

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.