[tech] Typespec for API first development

About Typespec

Typespec is a new language from Microsoft to define APIs. I've been prototyping with typespec for a few projects and am already sold on the benefits. I’ll introduce the language and tool ecosystem in this article.

In a nutshell, typespec allows you to define a schema with models (entities) and operations (actions) at a high level. Typespec can emit API schemas for targets like OpenAPI 3, Protobuf and others. I've been exclusively using OpenAPI for many years and even for a single protocol, typespec brings many benefits.

model Store {
  name: string;
  address: Address;
}

model Address {
  street: string;
  city: string;
}

@route("/stores")
interface Stores {
  list(@query filter: string): Store[];
  read(@path id: Store): Store;
}        

Advantages

Concise language

Typespec is inspired by typescript and is fairly concise. The generated OpenAPI for my projects was around twice in length compared to what was declared in typespec. In OpenAPI, repeating the same errors becomes cumbersome while this was easier in typespec.

Typespec supports namespaces, cross file imports, model composition via inheritance and templating (copying). This rich language helps make APIs concise relative to YAML for OpenAPI.


Tooling

The typespec compiler can be installed as a node package. There are libraries for http, rest and OpenAPI which provide enhanced functionality. The compiler provides errors and warnings making it easy to get quick feedback and iterate quickly.

VS Code has good tooling support for the language as of now along with cli based tools. If your target output is OpenAPI specs, you can continue using any existing tooling in your toolchain as well, like code generators from OpenAPI.

A formatter is provided as part of standard tooling making consistency possible.

Tooling is still not very mature though. Plugins for IDEs like intellij are still not available. However since typespec uses LSP tooling should follow soon.


APIs at scale

Typespec will really provide value in companies with different distributed teams working on APIs. A central team can implement common models and codified standards. Other teams can then extend the models with custom features.

Code reviews and validation of types becomes much simpler with types compared to yaml files.


API versioning

Evolution of APIs is possible with first class versioning support. It is possible to have a single file with multiple versioned models, operations and more. Adding, removing renames and other changes are supported via the library extensions. A versioned typespec specification can generate many independently versioned OpenAPI versions.


Extensibility with libraries

The typespec language is small and there is tooling for creation of libraries which can include

  • Typespec types for sharing definitions
  • Decorators similar to annotations that can add custom behavior
  • Emitters for different targets
  • Diagnostics, Linters for rule checks regarding API definitions like naming conventions and more.

Typespec libraries can be written in typescript or javascript. Based on hooks configured the library has access to the AST and APIs to be able to customize behavior. The current extensions for HTTP, REST, and OpenAPI3 are built as libraries and hence the extensibility tooling should be fairly mature.


Getting started

The getting started page and docs on the main page have good examples. For a tech talk view?

https://youtu.be/yfCYrKaojDo

For a sample advanced project with common models and versioning see the project I’ve been testing with - typespec-monorepo. I'll add additional patterns as I continue to explore typespec.


API First enabler

I'm a strong proponent of API first (compared to code first) approaches to development. Figuring out what to build before actually starting implementation helps build more intuitive APIs. Using tools like typespec can uplift how teams work with APIs making the process approachable and hence adoption of standards less painful.


Smithy is an alternative to typespec from Amazon. It has fairly similar goals and features. It was released publicly over a year ago. However the website does not have the finesse of the typespec project and I eventually ended up spending more time with typespec instead. Smithy also has a great ecosystem. Typespec is built using typescript, which smithy is java based and that might be a consideration for teams that need custom tooling. Either tool will help with an API first approach at scale.



Cross posted from blog - https://www.rahul.revos.name/p/tech-typespec-for-api-first-development



Shashi Shringari

Director | Software Development | Oracle Database | Oracle APEX | PL/SQL | Python

10 个月

Always get something new and interesting to learn from you :)

回复

要查看或添加评论,请登录

Rahul Revo的更多文章

  • [tech] Early 2025 AI thoughts

    [tech] Early 2025 AI thoughts

    Background The video above by Andrej Karpathy is a fantastic overview of how Large Language Models (LLMs) work for a…

  • Peak efficiency

    Peak efficiency

    While thousands of families are being impacted, I got this email in my inbox at around 9:46 AM today which is the…

  • Disagree and commit

    Disagree and commit

    Decisions Decision making is an integral aspect of how companies and teams function. In software technology based…

  • Consistency models for service data

    Consistency models for service data

    Context Most software services deal with data, storing and retrieving it as needed. However, ensuring this data remains…

  • DIY Photo frame

    DIY Photo frame

    The new year holidays gave me some free time to build a DIY photo frame. We had a smaller google device but needed…

  • Change

    Change

    Thinking in systems One of the most insightful books I read this year is Thinking in Systems. The author explains what…

  • Gratification for architects

    Gratification for architects

    Gratification is the pleasurable emotional reaction of happiness in response to a fulfillment of a desire or goal. As a…

  • Switching to a dumb phone (sort of)

    Switching to a dumb phone (sort of)

    Recently I started using a feature phone - Nokia 2780 flip instead of a regular smartphone. I'm not that great at…

  • America's skilled immigration crisis

    America's skilled immigration crisis

    Modern America has been made possible to a large extent by the contributions of thousands of immigrants. America has…

    2 条评论
  • [tech] Domain Driven Design

    [tech] Domain Driven Design

    Domain Driven Design (DDD) is a book by Eric Evans, known as “the blue book”, which provides a broad framework for…

社区洞察

其他会员也浏览了