How to securely use Azure Speech services in SPA app

CiaranODonnell 20 Reputation points
2025-09-11T15:56:05.2533333+00:00

Hello, I am building a SPA app (using Angular) and I want to add a talking avatar to it. I have the microsoft demo application for the Chat avatar and this functionality is what I want to achieve, however, the demo wants you put an API key into the form so it can call the Azure Speech services.

I don't want to have my API key on the client, so I wondered if there is a better approach.

I don't know if its possible but I was hoping that I could generate a short lived token on my backend services that I could pass to the client and it could use that with the Azure Speech APIs. I know you can get a token from a managed identity, is that the way to do it? Could I generate that token on the backend and pass it to the client and have it use that against the Speech api?

Azure Speech in Foundry Tools
0 comments No comments

Answer accepted by question author
Jerald Felix 18,760 Reputation points Volunteer Moderator
2025-09-12T00:25:29.44+00:00

Hello CiaranODonnell,

That is an excellent question and you are absolutely on the right track. Your instinct that you should not put API keys on the client is correct, and your proposed solution of using a backend service to generate and provide a short-lived token is the exact recommended security pattern for this scenario.

Exposing the subscription key in a Single Page Application (SPA) would allow anyone to copy it and use your Azure Speech resource, leading to unexpected costs and security risks. The token-based approach you described solves this perfectly.

Here is the step-by-step architecture for how to implement this securely:

  1. Create a Secure Backend Endpoint (Your Token Provider)

You need to create a simple API on your backend that your Angular app can call. This endpoint will act as a broker, securely exchanging your long-term Speech API key for a short-lived token that is safe to use on the client.

Backend Workflow:

  1. Create the Endpoint: Build a secure API endpoint, for example, /api/get-speech-token. This endpoint must require authentication, so only legitimate users of your application can request a token.

Secure Your Key: Your backend service should retrieve the Azure Speech subscription key from a secure location. Do not hardcode it. The best practice is to store it in Azure Key Vault and have your backend service (e.g., an Azure Function or App Service) access it using a Managed Identity.

Request the Token: From your backend code, make a POST request to the Azure Speech token issuance endpoint.

URL: https://<YOUR_REGION>.api.cognitive.microsoft.com/sts/v1.0/issueToken (replace <YOUR_REGION> with your Speech resource's region, e.g., eastus).

  **Header:** Include the **`Ocp-Apim-Subscription-Key`** header, setting its value to your Speech API key.
  
     **Body:** The POST request should have an empty body.
     
     **Return the Token:** The Speech service will return a plain text authorization token (a JWT). Your backend endpoint should then return this token string in the response to your Angular app. This token is typically valid for 10 minutes.
     
  1. Configure the Speech SDK in Your Angular App

In your Angular application, you will now use this short-lived token to initialize the Speech SDK instead of the API key.

Frontend Workflow:

Fetch the Token: When you need to use the Speech service, your Angular app first makes an authenticated call to your /api/get-speech-token endpoint to retrieve the token.

Initialize SpeechConfig: Use the SpeechConfig.fromAuthorizationToken() method to configure the SDK. This is the key difference. Instead of using fromSubscription, you will use the token.

typescript
import

Your Question About Managed Identity

You asked if you could use a token from a Managed Identity directly on the client. The answer is no, but you were very close to the full picture. A Managed Identity is for server-to-server communication. The correct way it fits into this pattern is:

Your backend service (like an Azure Function) uses its Managed Identity to securely access the Speech API key from Azure Key Vault. This avoids storing the key in your code or configuration files.

This two-step process—using a Managed Identity to secure the key on the backend, which then issues a temporary token for the frontend—is the most robust and secure architecture for SPAs.

You have a great handle on the security implications. This approach will work perfectly.

Best regards,

Jerald Felix

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.