Skip to content

API versioning - best practices & built-in support #2052

Closed

Description

@dkbast

When a product evolves, the API often does as well. Not allowing breaking changes can make life harder than necessary and lead to errors due to use of "deprecated" fields or endpoints.

Introducing more and more endpoints like suggested here can lead to a convoluted interface where it may not be instantly clear which endpoints are "currently recommended".

For that reason endpoints are often "versioned" and return something "completely" different for different versions.

I encountered a few different strategies for this over the past years:

  • "root" versioning (api.example.com/v1/foo/bar) - changing one endpoints signature increases the version for all endpoints
  • "per endpoint" versioning which is similar to root versioning, but changing one endpoint only increases its version the rest remains the same
  • "queryParam" versioning (api.example.com/foo/bar?v=1) (sometimes also done with header parameters) - basically the same as above but does not use the path for versioning.

Current state
Since Serverpod generates the endpoints for us we can currently use "per endpoint" versioning by copying the Endpoint/ Method in the Endpoint and changing the copy. Downside is that would still have access to all "old" endpoints/methods in the most current version of the client - we could probably try annotating with @deprecated, but it looks to me like we have no way to enforce that new functionality does not try to use old endpoints?

Other options
In other projects we have been using an http interceptor to add a version parameter to all requests - maybe something similar could be achieved by adding versioning into the "Endpoint" class?

Another idea could be to generate different (frontend) clients for each "version" and in the server code have a "builder/factory" which builds the endpoints for different versions (functional switch) - the builder would get a "config" object and return a suitable endpoint which we as devs write. A versioning mechanism would be necessary (e.g. pubspec.yaml if used with a registry or git tags if used in a mono repo?)

Conclusion
For now I think adding a few lines to the docs could be enough, but when targeting "enterprise" customers it would be great to have a versioning solution (or at least a recommended best practice) at hand.

Activity

  1. dkbast commented on Mar 25, 2024

    @dkbast
    ContributorAuthor

    I must have overlooked #1711 - we both discuss something very similar here

  2. SandPod commented on Mar 26, 2024

    @SandPod
    Contributor

    I'll close this issue in favor of #1711 since the feature request is around the same topic.
    Will mention this issue there for more thoughts on the subject.

    Please reopen if you don't agree with the decision with a motivation why :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions