manage-versions
description: Manage multiple API versions when using Speakeasy
sidebar_position: 5
Manage API Versions
The Speakeasy SDK provides both a global and per Api configuration option. If you want to use the SDK to track multiple Apis or Versions from the same service you can configure individual instances of the SDK.
- Go
- Java
- Typescript
- Rust
import "github.com/speakeasy-api/speakeasy-go-sdk"
func main() {
r := mux.NewRouter()
// Configure a new instance of the SDK for the store API
storeSDKInstance := speakeasy.New(speakeasy.Config {
APIKey: "YOUR API KEY HERE", // retrieve from Speakeasy API dashboard.
ApiID: "store_api", // this is an ID you provide that you would like to associate captured requests with.
VersionID: "1.0.0", // this is a Version you provide that you would like to associate captured requests with.
})
// Configure a new instance of the SDK for the product API
productSDKInstance := speakeasy.New(speakeasy.Config {
APIKey: "YOUR API KEY HERE", // retrieve from Speakeasy API dashboard.
ApiID: "product_api", // this is an ID you provide that you would like to associate captured requests with.
VersionID: "1.0.0", // this is a Version you provide that you would like to associate captured requests with.
})
// The different instances of the SDK (with differnt IDs or even versions assigned) can be used to associate requests with different APIs and Versions.
s := r.PathPrefix("/store").Subrouter()
r.Use(storeSDKInstance.Middleware)
s := r.PathPrefix("/products").Subrouter()
r.Use(productSDKInstance.Middleware)
}
Create your own WebMvcConfigurer
implementation for each instance of the SDK, like the example below:
package dev.speakeasyapi.sdk;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import dev.speakeasyapi.sdk.utils.SpeakeasyFilter;
import dev.speakeasyapi.sdk.utils.SpeakeasyInterceptor;
@Configuration
@ConfigurationProperties(prefix = "myspeakeasyinstancev1")
@ConfigurationPropertiesScan
@Import(SpeakeasyFilter.class) // This enables request and response capture and is a requirement for the SDK to work
public class MySpeakeasyInstanceV1 implements WebMvcConfigurer {
private String apiKey;
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new SpeakeasyInterceptor(apiKey, "users", "v1.0.0"))
.addPathPatterns("/v1/users/**") // Match paths
.excludePathPatterns("/v1/products/**"); // Exclude paths
}
}
Express
import { Config, SpeakeasySDK } from "@speakeasy-api/speakeasy-typescript-sdk";
import express from "express";
const app = express();
// Configure a new instance of the SDK for the store API
const storeSDK = new SpeakeasySDK({
apiKey: "YOUR API KEY HERE", // retrieved from Speakeasy API dashboard.
apiID: "store_api", // this is an ID you provide that you would like to associate captured requests with.
versionID: "1.0.0", // this is a Version you provide that you would like to associate captured requests with.
port: 3000, // The port number your express app is listening on (required to build full URLs on non-standard ports)
});
// Configure a new instance of the SDK for the product AP
const productSDK = new SpeakeasySDK({
apiKey: "YOUR API KEY HERE", // retrieved from Speakeasy API dashboard.
apiID: "product_api", // this is an ID you provide that you would like to associate captured requests with.
versionID: "1.0.0", // this is a Version you provide that you would like to associate captured requests with.
port: 3000, // The port number your express app is listening on (required to build full URLs on non-standard ports)
});
// The different instances of the SDK (with differnt IDs or even versions assigned) can be used to associate requests with different APIs and Versions.
const storeRouter = app.route("/store");
storeRouter.use(storeSDK.expressMiddleware());
const productsRouter = app.route("/products");
productsRouter.use(productSDK.expressMiddleware());
// Rest of your express app setup code
NestJS
import { Config, SpeakeasySDK } from '@speakeasy-api/speakeasy-typescript-sdk';
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
// Configure a speakeasy SDK instance
const cfg: Config = {
apiKey: "YOUR API KEY HERE", // retrieve from Speakeasy API dashboard.
apiID: "YOUR API ID HERE", // custom Api ID to associate captured requests with.
versionID: "YOUR VERSION ID HERE", // custom Version ID to associate captured requests
port: 3000, // The port number your express app is listening on (required to build full URLs on non-standard ports)
};
const sdk = new SpeakeasySDK(cfg);
// Configure the NestJS middleware for the routes of you Controller
consumer.apply(sdk.nestJSMiddleware()).forRoutes(AppController);
}
}
Coming Soon!