How to get backend service URL using backend identifier in Azure API Management

I’m working with Azure API Management and need help with policy expressions. I have a backend service configured in my APIM instance and I know its backend identifier. The problem is I need to dynamically fetch the URL of this backend service within my policy.

Here’s what I’m trying to achieve:

<set-variable name="serviceUrl" value="@( RetrieveBackendService("backend-123") )" />

I’m looking for the correct function or method that should replace my placeholder RetrieveBackendService. Is there a built-in policy expression that can help me get the backend URL when I only have the backend ID? I’ve checked the documentation but couldn’t find a clear example of this specific use case.

Any guidance on the proper syntax would be really helpful. Thanks in advance!

hey! no direct method to fetch backend URLs in policies. i usually keep em as named values, ref like @(context.Deployment.ServiceUrl), or create a custom backend resolver. it’s a bit of a hack, but it does the job!

Unfortunately, Azure API Management does not provide a built-in policy expression specifically to retrieve a backend service URL using only the backend ID. Instead, I recommend directly using the set-backend-service policy statement with your backend ID, like this: . This method will route your request to the appropriate backend. If you require the backend URL for other aspects of your policy, consider storing the backend URLs as named values in APIM and reference them accordingly. This approach allows you to modify URLs easily without changing the policies while maintaining the dynamic nature you seek.

Interesting challenge! You could use the management API within your policy to retrieve backend details. But I’m curious - why do you need the actual URL instead of just routing to the backend? There might be a different approach that works better.