0

Can I create an API that has the definition of the sum of two numbers and returns me the output. I want to write all the logic in Azure Web API Management itself. Is there any provision, or do I need to create it in my machine and import it to Azure Web API Management?

Is it possible to create it in Web API in Azure itself, rather than importing it?

1 Answer 1

1

There are two ways to go about this. APIM does support policy expressions: https://learn.microsoft.com/en-us/azure/api-management/api-management-policy-expressions This allows you to plug in arbitrary code into request processing pipeline. You can check policy samples here: https://learn.microsoft.com/en-us/azure/api-management/policy-samples to see this in action. When combined with other policies it does allow you to a lot of things. If we assume that you "addition" operation has URI template of /add?a={a}&b={b} then you can sum up and return result with one simple policy:

<return-response> <set-status code="200" reason="OK" /> <set-body>@{ var a = int.Parse(context.Request.Url.Query.GetValueOrDefault("a", "0")); var b = int.Parse(context.Request.Url.Query.GetValueOrDefault("b", "0")); return (a + b).ToString(); }</set-body> </return-response> 

As you can see this is a pretty much regular C# code, but it's limited in what you can do and what types you can use (see first link). If you can't make it work within these limitations your best bet is to move custom logic outside of APIM, into Azure Functions, for example.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Vitaliy, I have done it the way you described...I have created a Azure API Function and then I have imported it while creating my New API. So Is the right way to do it?
Depends on your needs. If code is complex enough it's certainly easier to maintain it as Azure function, the downside is increased latency, but that is not necessary a problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.