This guide demonstrates how to create an Azure AI Language service in the Azure portal and call its sentiment analysis capability using both the Python SDK and the REST API. You’ll see that the SDK provides a more concise developer experience, while the REST example shows the underlying HTTP payloads and is useful when SDKs are unavailable. What you’ll learn:Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
- How to create an Azure AI service (multi-service account vs. single dedicated service)
- Where to find endpoints and keys
- Example code for sentiment analysis using the Python SDK
- Example code for sentiment analysis using the REST API
- When to choose SDK vs. REST
Create an Azure AI service in the portal
If you already have a multi-service account, it exposes multiple capabilities (OpenAI, Speech, Vision, Language, etc.) under the same account-level keys. The portal lists AI service resources like this:

- Select a subscription and resource group (e.g., rg-ai102-get-started-sdk)
- Choose a region (e.g., East US)
- Provide a globally unique resource name (this becomes <service-name>.cognitiveservices.azure.com)
- Pick a pricing tier (for example S1)


Do NOT embed long-lived keys directly in source code for production. Use Azure Key Vault, managed identities, or environment variables to secure secrets.
Choose: SDK vs REST
Both approaches return a sentiment label and confidence scores. Use SDKs when available for a cleaner, idiomatic interface and automatic authentication helpers. Use REST when SDKs are not available or you need direct HTTP access. Comparison at a glance:| Resource | Use case | Pros |
|---|---|---|
| Python SDK (azure-ai-textanalytics) | Typical development on Python | Concise code, structured objects, handles auth and retries |
| REST API (HTTP POST) | Direct HTTP integrations, non-supported languages | Shows exact payload and headers, no SDK dependency |
- Azure AI Language service overview
- Azure SDK for Python - Text Analytics docs
- Language REST API reference (analyze-text)
SDK approach (Python)
Install the SDK packages: pip install azure-core azure-ai-textanalytics Example Python SDK usage. Replace endpoint and key with your values (do not hard-code in production).- Document-level sentiment (positive / neutral / negative)
- Confidence scores for each class
- Optional per-sentence sentiment and additional metadata if requested
REST approach (Python + requests)
The REST approach requires building the analyze-text URL and POSTing a JSON body. Ensure boolean values in the JSON are proper booleans (true / false), not strings. Use the endpoint that you copied from Keys and Endpoint. The endpoint should usually end with a trailing slash (or adjust URL concatenation accordingly). Example Python REST code:- The REST payload reveals the exact request structure (kind, parameters, analysisInput.documents).
- Set “opinionMining”: true to enable opinion mining in results; omit or set false if not needed.
- The header shown uses Ocp-Apim-Subscription-Key; depending on your resource type, you may also see header variants (follow the current Azure REST docs).
Comparing results and examples
Both SDK and REST return a sentiment label and confidence scores. Example inputs and typical outcomes:- “Learning AI is good for career growth.” — typically returns positive with a high positive confidence score.
- “The food and service were unacceptable.” — typically returns negative.
- Mixed content — e.g., “Hotel is awesome. The food and service were unacceptable.” — shows how per-sentence analysis can reveal mixed sentiments inside a single document.
Best practices & next steps
- For production, never hard-code credentials. Use:
- Azure Key Vault
- Managed identities (when running in Azure)
- Environment variables with secure deployment pipelines
- Prefer SDKs for simpler, cleaner code and better integration with client libraries.
- Use REST for custom clients, language/platforms without an SDK, or to inspect raw payloads.
Always restrict and rotate keys regularly. Grant the minimum required permissions and monitor usage for unexpected calls.