Microsoft API Guidelines

repository·vNext·Indexed 12 days ago

https://github.com/microsoft/api-guidelines

Standardized REST API design guidelines used by Microsoft service teams to ensure consistency and interoperability. Includes specialized guidance for Azure and Microsoft Graph, covering RESTful interfaces, OpenAPI style standards, versioning policies, naming conventions, and patterns for long-running operations (LROs).

Tokens
66.9K
Snippets
126
Records
263
Agent score
96%

What's inside Microsoft API Guidelines

  1. Overview of Microsoft REST API Guidelines

    vNext

    The Microsoft REST API Guidelines are internal company-wide design principles used to ensure that RESTful HTTP interfaces are developed consistently across Microsoft.

    Core Principles:

    • Accessibility: Encourages application developers to have resources accessible via a RESTful HTTP interface.
    • Consistency: REST APIs SHOULD follow consistent design guidelines to make them easy and intuitive for developers to use across different platforms.
  2. Access Microsoft REST API Guidelines

    vNext
    The Microsoft REST API Guidelines repository provides standardized guidance for designing and building RESTful APIs. It is divided into specific guidance sets depending on the target service type (Azure or Microsoft Graph).
  3. Understand the Microsoft Graph REST API Guidelines

    vNext

    The Microsoft Graph REST API Guidelines are a set of mandatory and recommended standards for Microsoft Graph API producer teams. The goal is to ensure the API surface is developer-friendly, works well with multi-language SDKs, and remains sustainable and evolvable through clear API contracts.

    Key design goals include:

    • Using consistent naming, patterns, and web standards (HTTP, REST, JSON).
    • Ensuring compatibility with various programming language SDKs.
    • Maintaining evolvability via clear API contracts.
  4. Understand the Microsoft Azure REST API Guidelines

    vNext

    The Microsoft Azure REST API Guidelines provide prescriptive standards for Azure service teams implementing data plane APIs. The goal is to ensure APIs are developer-friendly, efficient, SDK-compatible, fault-tolerant (supporting retries, idempotency, and optimistic concurrency), and sustainable through clear versioning contracts.

    Key Requirements for API Contracts:

    1. Customer workloads must never break due to a service change.
    2. Customers must be able to adopt a new version without requiring code changes.

    Important Distinctions:

    • Data Plane APIs: Follow these guidelines.
    • Management Plane (ARM) APIs: Do not use these guidelines; instead, refer to the Azure Resource Manager Resource Provider Contract.
    • Existing GA'd Services: Do not change or break existing APIs. Use these concepts for future APIs while prioritizing consistency within your existing service.
  5. Access Azure REST API design guidance and best practices

    vNext

    The Microsoft REST API Stewardship Board provides a collection of resources to help developers design APIs that are easy to use, scalable, maintainable, and consistent. Use these resources to ensure your service design follows industry best practices and provides a high-quality developer experience.

    Key resources include:

    • Service Design Considerations: High-level guidance for designing services.
    • REST API Guidelines: Specific technical rules for RESTful interfaces.
    • OpenAPI Style Guidelines: Standards for defining APIs using the OpenAPI specification.
    • Versioning Policy: Guidance on versioning for Azure services, SDKs, and CLI tools.
    • Breaking Changes: Information regarding how breaking changes are handled.
  6. What is the Flat bag of properties pattern?

    vNext

    The Flat bag of properties is a Microsoft Graph API design pattern used to model a small number of variants of a common concept as a single entity type. Instead of using complex type hierarchies, the designer includes all potential properties for all variants within one type, plus a discriminator property (often called variantType or type) to distinguish which variant the entity represents.

    For any given value of the discriminator property, only a specific subset of the properties is meaningful; all other properties are ignored by the consumer. This pattern simplifies query construction, particularly when using OData $filter expressions, because it avoids the need for type casting.

  7. What is a Long-Running Operation (LRO) and when to use one

    vNext

    A Long-Running Operation (LRO) is an operation that is initiated synchronously but must execute asynchronously because the service cannot maintain a long-lived connection (typically > 1 second) due to load-balancer timeouts.

    When to implement an LRO:

    • Implement an LRO if the 99th percentile response time is greater than 1 second.
    • Use an LRO when the client needs to poll the service to track progress/completion.

    Constraints:

    • DO NOT implement PATCH as an LRO. If update semantics are required, use the LRO POST action pattern instead.
    • DO NOT use a long-running POST to create a resource; use PUT instead.
  8. What is the Facets pattern in Microsoft Graph

    vNext

    The Facets pattern is used to model heterogeneous resources that share common properties but also possess unique characteristics belonging to different variants. Instead of using a strict type hierarchy, an entity type is defined with multiple complex properties (facets).

    Key characteristics:

    • Non-mutually exclusive variants: A single resource can express multiple variants at once (e.g., a resource can be both a file and an image).
    • Identification by presence: A resource is identified as a specific variant by the presence of its corresponding facet property in the object.
    • Simplified querying: This pattern allows for easier querying using OData $filter expressions because it avoids the need for complex type casting.

    Use this pattern when you have a large number of variants that overlap, rather than using a strict type hierarchy or a flat bag of properties.

  9. What is the Dictionary pattern in Microsoft Graph?

    vNext

    The Dictionary pattern allows an API to provide a set of key/value pairs where the keys are dynamically specified by the API consumer rather than the service. This is useful when a resource needs to include an unknown quantity of semantically related data values.

    Criteria for use

    Use a dictionary when:

    • The data values are semantically related as a collection.
    • The values are either primitive types or complex types.
    • The client defines the keys, not the service.

    Key Characteristics

    • Representation: In JSON payloads, dictionaries are represented as JSON objects where property names are the keys and their values are the corresponding values.
    • Null Values: Dictionaries do not support null values for entries. To remove an entry, you must set its value to null via a PATCH request.

    Alternatives

    • Use Open extensions if you want to allow clients to extend Microsoft Graph.
    • Use Complex types if the set of data values is known in advance.
  10. What is a Navigation Property?

    vNext

    A navigation property is an OData-compliant mechanism used to identify relationships between resources. Instead of using weakly-typed foreign key properties (like userId), navigation properties allow you to traverse relationships via URL path segments. This enables clients to discover and access related resources without needing to manually construct URLs or perform multiple lookups for IDs.

    Key benefits:

    • Discoverability: Relationships are part of the API structure.
    • Efficiency: Allows retrieving related entities in a single round trip using the $expand query parameter.
    • Strong Typing: Supports automated SDK generation and documentation.

    Common URL patterns:

    • Many-to-one: /user/{userId}/manager
    • One-to-many: /user/{userId}/messages
  11. What is the Viewpoint pattern?

    vNext

    The Viewpoint pattern is a Microsoft Graph API design pattern used to manage properties of a shared object that have different values for different users.

    In a shared resource (like a group message or a website), different users may have different individual states. For example, one user might have read a message while another has not. Instead of forcing the client to manage this state locally, the API provides a viewpoint structural property on the entity. This allows the server to persist and synchronize the user's specific context across multiple devices.

    Key characteristics:

    • User-specific context: The viewpoint property reflects an individual user's state.
    • Application Permissions: Because viewpoint is tied to an individual user, the property is null when accessed using application permissions.
    • Server-side computation: Viewpoints can be computed on the server. In such cases, the API should include OData annotations (e.g., Org.OData.Core.V1.Computed) to inform downstream tools.
  12. What is the Upsert pattern in Microsoft Graph

    vNext

    The Upsert pattern is a non-destructive, idempotent operation that uses a client-provided key to ensure system resources can be deployed reliably and repeatedly. It is primarily designed for Infrastructure as Code (IaC) and desired state configuration scenarios where a client needs to ensure a resource exists with a specific identity without manually checking for its existence first.

    How it works

    • Mechanism: It uses a PATCH request with a client-provided key in the URL.
    • Behavior:
      • If the resource (identified by the key) does not exist, the service performs a "create" (insert) and generates a primary key if necessary.
      • If the resource already exists, the service performs an "update".
    • Keys:
      • If a natural primary key exists, it is used.
      • If the primary key is service-generated, an alternate key (often named uniqueName in IaC contexts) is used to support idempotent creation.
    • Idempotency: Unlike standard POST creation which might return a 409 Conflict if a resource already exists, an Upsert via PATCH will return a 200 OK for updates or a 201 Created for new resources, making it safe to run multiple times.
    PATCH /groups(uniqueName='Group157')
    Prefer: return=representation
    
    {
        "displayName": "My favorite group",
        "description": "All my favorite people in the world"
    }