Azure SDK for JavaScript

AI Quickstart

Get up and running with the Azure OpenAI service using the official JavaScript SDK.

Prerequisites

Install the SDK

npm install @azure/openai

Sample Code

import { OpenAIClient, AzureKeyCredential } from "@azure/openai";

const endpoint = "YOUR_ENDPOINT";
const apiKey = "YOUR_API_KEY";

const client = new OpenAIClient(endpoint, new AzureKeyCredential(apiKey));

async function ask(question) {
  const result = await client.getCompletions("deployment-name", [
    { role: "user", content: question }
  ]);
  return result.choices[0].message.content;
}

// Example usage
ask("Explain quantum computing in simple terms.")
  .then(console.log)
  .catch(console.error);

Try it in the browser