How to call an AI model from an API — in 60 seconds
Want to plug AI into your app, script, or workflow? Calling a model from an API is way easier than it sounds.
Here’s the simple flow:
- 1. Get an API key: Sign up with your AI provider (like OpenAI or Azure OpenAI), then grab your secret API key from the portal.
- 2. Find the endpoint: Look for something like
/chat/completionsor/responsesin the docs—this is the URL your app will call. - 3. Send a POST request: Use
curl, Postman, or your favorite language (Python, JavaScript, C#) to send JSON with your model name and prompt. - 4. Include your key securely: Put the API key in an
Authorizationheader (never hard-code it in public code or front-end apps). - 5. Parse the response: Read the JSON result, grab the generated text/content field, and plug it back into your app.
Example: A simple POST with
{ "model": "gpt-4", "input": "Summarize this text..." } returns a JSON object that includes your summary string.Bottom line: Call URL → send JSON → read JSON. That’s the core pattern behind almost every AI-powered app.
Comments
Post a Comment