Getting Started with Azure OpenAPI

Author

Reads 1K

Photo Of Person Using Computer
Credit: pexels.com, Photo Of Person Using Computer

Azure OpenAPI is a powerful tool for designing, building, and documenting RESTful APIs. It's a great choice for developers who want to create scalable and maintainable APIs.

To get started with Azure OpenAPI, you'll first need to install the Azure OpenAPI extension for Visual Studio Code. This extension provides a set of tools and features that make it easy to work with OpenAPI definitions.

You can find the Azure OpenAPI extension in the Visual Studio Code marketplace. Simply search for "Azure OpenAPI" and click the "Install" button to get started.

Once you've installed the extension, you can create a new OpenAPI definition by clicking on the "Azure OpenAPI" icon in the Visual Studio Code sidebar. This will open a new file with a basic OpenAPI definition template.

Prerequisites

To get started with Azure OpenAPI, you'll need to have a few things in place.

First and foremost, you'll need an existing API Management instance. If you haven't already, go ahead and create one.

Credit: youtube.com, Import OpenAPI definitions from Azure API Management to Postman

You'll also need an Azure OpenAI resource with a model deployed. Take note of the ID (name) of the deployment, as you'll need it later when testing the imported API in API Management.

Finally, make sure you have the necessary permissions to grant access to the Azure OpenAI resource from the API Management instance.

Importing OpenAPI

Importing OpenAPI is a straightforward process in Azure API Management. You can import an OpenAPI specification from a URL or a path to a specification using the az apim api import command.

To import an OpenAPI specification from a URL, you can use the az apim api import command with the --specification-url parameter. This command imports an OpenAPI specification from the specified URL to an API Management instance named apim-hello-world.

If you need to update the settings of the imported API, you can use the az apim api update command. This command updates the settings of the API Management instance named apim-hello-world.

Credit: youtube.com, Demo: OpenAPI Extension Demo and Ask the Experts | Azure Functions: Discover OpenAPI and Power Apps

You can also import an OpenAPI specification from a path to a specification using the --specification-path parameter. This parameter allows you to import the specification from a local file or a path to a specification.

To import an OpenAPI specification using the Import-AzApiManagementApi Azure PowerShell cmdlet, you can use the -SpecificationPath parameter. This cmdlet imports an OpenAPI specification from the specified path to a specification to an API Management instance named apim-hello-world.

After importing the API, you can update the settings using the Set-AzApiManagementApi cmdlet. This cmdlet updates the settings of the API Management instance named apim-hello-world.

Adding OpenAPI

Adding OpenAPI to Azure API Management is a straightforward process. You can manually download the OpenAPI specification for the Azure OpenAI REST API and add it to API Management as an OpenAPI API.

To do this, navigate to your API Management instance in the Azure portal and select APIs > + Add API. Under Define a new API, select OpenAPI and enter a Display name and Name for the API.

Credit: youtube.com, Functions OpenAPI Tutorial

You'll also need to enter an API URL suffix ending in /openai to access the Azure OpenAI API endpoints in your API Management instance. This is typically in the format of my-openai-api/openai.

Here's a step-by-step guide to adding an OpenAPI specification to API Management:

  1. In the Azure portal, navigate to your API Management instance.
  2. In the left menu, select APIs > + Add API.
  3. Under Define a new API, select OpenAPI.
  4. Enter a Display name and Name for the API.
  5. Enter an API URL suffix ending in /openai.
  6. Select Create.

Once you've completed these steps, the API is imported and displays operations from the OpenAPI specification.

Configuring OpenAPI

To define the swagger.json endpoint, you need to create an HTTP Trigger Function and delete its contents. Then, use the generateOpenApi3_1Spec helper function from the npm package to define the shared metadata and components that your OpenAPI spec requires.

This Function will be merged with the annotated Functions at runtime, so you need to define the shared components first. You can start by telling consumers about the API, which requires defining the info object with title and version.

The minimum information you need to provide is the title and version of your API, but since you're using $ref to reference a shared component schema, you should define that as well. For example, you can define a Game schema with properties such as id, state, questions, players, and answers.

Here's a list of the properties you can define in the Game schema:

Configure Authentication

Credit: youtube.com, Spring Boot + Swagger 3 (OpenAPI 3) + Security Example

Configuring authentication for Azure OpenAI API is a straightforward process. If you've imported the Azure OpenAI API directly to your API Management instance, authentication using the API Management instance's managed identity is automatically configured.

You can use an API key or a managed identity to authenticate to the Azure OpenAI API. For API keys, you'll need to provide the key when making API calls.

If you added the Azure OpenAI API from its OpenAPI specification, you'll need to configure authentication manually. This involves using API Management policies to set up authentication.

Defining Swagger.json Endpoint

To define a Swagger.json endpoint, you'll need to create a new HTTP Trigger Function in Azure Functions. Delete its contents and use the `generateOpenApi3_1Spec` helper function from the `@aaronpowell/azure-functions-nodejs-openapi` npm package.

You can import the `generateOpenApi3_1Spec` function and export it as the default export, like this: `import { generateOpenApi3_1Spec } from "@aaronpowell/azure-functions-nodejs-openapi"; export default generateOpenApi3_1Spec({});`

This will define the shared metadata and components that your OpenAPI spec requires, which will be merged with the annotated functions at runtime.

Credit: youtube.com, Swagger API documentation tutorial for beginners 2023

To define the API, you'll need to provide some basic information, such as the title and version of your API. You can do this by adding an `info` object to the `generateOpenApi3_1Spec` function, like this: `export default generateOpenApi3_1Spec({ info: { title: "Awesome trivia game API", version: "1.0.0" } });`

You'll also need to define any shared components, such as schema objects, that will be used throughout your API. For example, you can define a `Game` schema object like this: `export default generateOpenApi3_1Spec({ info: { title: "Awesome trivia game API", version: "1.0.0" }, components: { schemas: { Game: { type: "object", properties: { id: { type: "string", description: "Unique identifier for the game" }, state: { type: "string", description: "The status of the game", enum: ["WaitingForPlayers", "Started", "Complete"] }, questions: { type: "array", items: { $ref: "#/components/schemas/Question" } }, players: { type: "array", items: { $ref: "#/components/schemas/Player" } }, answers: { type: "array", items: { $ref: "#/components/schemas/PlayerAnswer" } } } } } });`

By defining these shared components, you can reference them throughout your API using the `$ref` keyword.

Here's an example of what the complete `generateOpenApi3_1Spec` function might look like:

```javascript

import { generateOpenApi3_1Spec } from "@aaronpowell/azure-functions-nodejs-openapi";

export default generateOpenApi3_1Spec({

Credit: youtube.com, Openapi 3.0 / Swagger editor tutorial for beginners | Working demo | Simple explanation

info: {

title: "Awesome trivia game API",

version: "1.0.0"

},

components: {

schemas: {

Game: {

type: "object",

properties: {

id: {

type: "string",

description: "Unique identifier for the game"

},

state: {

type: "string",

description: "The status of the game",

enum: ["WaitingForPlayers", "Started", "Complete"]

},

questions: {

type: "array",

items: {

$ref: "#/components/schemas/Question"

}

},

players: {

type: "array",

items: {

$ref: "#/components/schemas/Player"

}

},

answers: {

type: "array",

items: {

$ref: "#/components/schemas/PlayerAnswer"

}

}

}

}

}

}

});

```

Note that this is just an example, and you'll need to customize the `generateOpenApi3_1Spec` function to fit your specific use case.

Testing OpenAPI

Testing OpenAPI is a crucial step to ensure your Azure OpenAI API is working as expected. You can test it in the API Management test console by supplying a model deployment ID configured in the Azure OpenAI resource.

To get started, select the API you created and go to the Test tab. From there, select an operation that's compatible with the model you deployed. The page will display fields for parameters and headers. Enter the necessary values, including any query parameters or request body, and select Send.

Credit: youtube.com, REST API and OpenAPI: It’s Not an Either/Or Question

When the test is successful, the backend responds with a successful HTTP response code and some data. Appended to the response is token usage data to help you monitor and manage your Azure OpenAI API consumption.

You can also test the new API in the portal, which provides a convenient way for administrators to view and test the operations of an API. To do this, select the API you created and go to the Test tab. Select an operation, and the page will display fields for query parameters and headers.

Depending on the operation, enter query parameter values, header values, or a request body. Select Send, and the test console will send a request to API Management's CORS proxy, which forwards the request to the API Management instance, which then forwards it to the backend.

Here are the steps to test the Azure OpenAPI:

  1. Select the API you created in the previous step.
  2. Select the Test tab.
  3. Select an operation that's compatible with the model you deployed in the Azure OpenAI resource.
  4. Enter other parameters and headers as needed.
  5. Depending on the operation, enter query parameter values, header values, or a request body.
  6. Select Send.

Frequently Asked Questions

What is OpenAPI in Azure?

OpenAPI in Azure is a standardized format for describing RESTful APIs, enabling easy development and consumption of APIs by mapping resources and operations. It's a key component in building and documenting APIs in Azure, facilitating collaboration and automation.

Glen Hackett

Writer

Glen Hackett is a skilled writer with a passion for crafting informative and engaging content. With a keen eye for detail and a knack for breaking down complex topics, Glen has established himself as a trusted voice in the tech industry. His writing expertise spans a range of subjects, including Azure Certifications, where he has developed a comprehensive understanding of the platform and its various applications.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.