If you want to skip reading the text that follows and simply want to download Visual Studio Code Snippets for Azure API Management policies, click here.
Azure API Management gives you a framework for publishing your APIs in a consistent manner with built-in benefits like developer engagement, business insights, analytics, security, and protection. However, the most powerful capability it offers is the ability to customize behavior of the API itself. Think of the customization as a short program that gets executed just before or after your API is invoked. The short program is simply a collection of statements (called policies in Azure API Management). Examples of policies that come out of the box include format conversion from XML to JSON, applying rate and quota limits and enforcing IP filtering. In addition, you have control flow policies such as choose that is similar to if-then-else, or a switch construct and set-variable that allows you declare a context variable. Finally, you have the ability to write C# (6.0) expressions. Each expression has access to the context variable, as well as, allowed to leverage a subset of .NET Framework types. As you can see, Azure API Management policies offer constructs equivalent to a programming language.
This begs the question, how do you author Azure API Management policies?
Well, today you have two options.
- Policy editor built directly in the portal, as shown below:You have the policy statements available on the right. You have policy definition pane in the center where you can drag and drop a policy, as well as author a policy statement.
- Develop the policies in an IDE of your choice and rely on API Management Git integration, as depicted below:Each API Management service instance maintains a configuration database that contains information about the configuration including the policies.You can save the configuration settings to a git server repo. Once the settings have been saved to the server repository, you can make a local clone of it and make any necessary changes. Finally you can push the changes back to server repo and save it back to the configuration database
I tend to prefer the second option since it allows me to version control the policies and deploy them in an automated manner. I also prefer authoring my policies in Visual Studio Code. Since a lot of policy development is about building expression, Visual Studio Code Colorizer (syntax highlighting) comes in handy. However, what’s still missing is the “intellisense” for all the policies. Wouldn’t it be nice if you started typing the “ip” and you got the type completion for the ip-filer policy? Like so:
Fortunately, Visual Studio Code offers customizability in the form of code snippets. So we did just that. We created a snippet file for all of the API Management Policies. With this snippets file you can get policy statement completion as shown below.
You can get the code snippet file here. Simply place it under the under File > Preferences (Code > Preferences on Mac).
Please let us know your thoughts. Based on your feedback, we will consider sharing these snippets on the Visual Studio Code marketplace.