The OpenAI API provides a simple interface to state-of-the-art AI models for text generation, natural language processing, computer vision, and more. This example generates text output from a prompt, as you might using ChatGPT.
Copy
Ask AI
import OpenAI from "openai";const client = new OpenAI();const response = await client.responses.create({ model: "gpt-4.1", input: "Write a one-sentence bedtime story about a unicorn."});console.log(response.output_text);
Data retention for model responses
Response objects are saved for 30 days by default. They can be viewed in the dashboard logs page or retrieved via the API. You can disable this behavior by setting store to false when creating a Response.OpenAI does not use data sent via API to train our models without your explicit consent—learn more.
Give the model access to new data and capabilities using tools. You can either call your own custom code, or use one of OpenAI’s powerful built-in tools. This example uses web search to give the model access to the latest information on the Internet.
Copy
Ask AI
import OpenAI from "openai";const client = new OpenAI();const response = await client.responses.create({model: "gpt-4.1",tools: [ { type: "web_search_preview" } ],input: "What was a positive news story from today?",});console.log(response.output_text);
Use the OpenAI platform to build agents capable of taking action—like controlling computers—on behalf of your users. Use the Agent SDK for Python to create orchestration logic on the backend.
Copy
Ask AI
from agents import Agent, Runnerimport asynciospanish_agent = Agent( name="Spanish agent", instructions="You only speak Spanish.",)english_agent = Agent( name="English agent", instructions="You only speak English",)triage_agent = Agent( name="Triage agent", instructions="Handoff to the appropriate agent based on the language of the request.", handoffs=[spanish_agent, english_agent],)async def main(): result = await Runner.run(triage_agent, input="Hola, ¿cómo estás?") print(result.final_output)if __name__ == "__main__": asyncio.run(main())# ¡Hola! Estoy bien, gracias por preguntar. ¿Y tú, cómo estás?