Customize Namespaces
When you’re exposing your API to users, you may want to group API methods into namespaces to give your SDK an object-oriented interface. This type of interface can help users better conceptualize the objects they are manipulating when they use the API.
Default Behavior
By default, Speakeasy will use the tags
in your OpenAPI spec as the organizing principles for namespaces. For each tag
in your spec, Speakeasy will create a namespace.
Each method will then be added to namespaces corresponding with its tags
. If a method does not have an associated tag
, it will be added to the root SDK class of the generated client library. If a method has multiple tags associated with it, the operation will appear as a method in multiple classes.
The following example illustrates the method sdk.Drinks.ListDrinks()
assigned to the Drinks
namespace and another method sdk.ListLocations()
kept in the default class:
paths:/bar_locations:get:operationId: listLocationssummary: List all locations of the Speakeasy bardescription: Get a list of all the bars being run by Speakeasyresponses:"200":description: A list of barscontent:application/json:schema:type: arrayitems:$ref: "#/components/schemas/BarLocation"/drinks:get:operationId: listDrinkssummary: List all drinksdescription: Get a list of all drinks served by the bartags:- drinksresponses:"200":description: A list of drinkscontent:application/json:schema:type: arrayitems:$ref: "#/components/schemas/Drink"tags:- name: drinksdescription: Everything about our Drinks on offer
The generated SDK will include methods that can be invoked as follows:
// Method added into the Drinks namespacesdk.Drinks.ListDrinks()// Default methodsdk.ListLocations()
Define Namespaces Without Tags x-speakeasy-group
Sometimes the tags
in an OpenAPI spec may already be used for an unrelated purpose (for example, autogenerating documentation). In this scenario, you may want to use something other than tags
to organize your methods.
The x-speakeasy-group
field allows you to define custom namespaces. Add this field to any operation in your OpenAPI spec to override any tags
associated with that method. For example:
paths:/drinks/{drink_type}/get_vintage:get:operationId: getVintagesummary: Check the vintage of the winedescription: Get the vintage of a drink served by the barparameters:- name: drink_typein: pathdescription: The type of drinkrequired: trueschema:type: stringtags:- drinksx-speakeasy-group: wineresponses:"200":description: A list of drinkscontent:application/json:schema:type: arrayitems:$ref: "#/components/schemas/Drink"tags:- name: drinksdescription: Everything about Drinks on offer
The generated SDK will include a method, which can be invoked as follows:
// GetVintage - get the vintage of the winesdk.wine.GetVintage("wine")
Define Multi-Level Namespaces
You can use tags
or the x-speakeasy-group
extension to define nested namespaces for your operations using .
notion. There is no limit to the number of levels you can define.
paths:/drink/{drink_type}/get_vintage/:get:operationId: getVintagesummary: Check the vintage of the winetags:- drinks.wineparameters:- name: drink_typein: pathdescription: The type of drinkrequired: trueschema:type: stringresponses:"200":description: the wine vintagecontent:application/json:schema:$ref: "#/components/schemas/Vintage"
The generated SDK will include a method, invoked as follows:
// Get the Vintage of a specified wine.sdk.Drinks.Wine.GetVintage("wine")
Multiple Namespaces
If you want to add a method to multiple namespaces, list multiple values in tags
or the x-speakeasy-group
extension. Both accept an array of values:
paths:/drink/{drink_type}/get_vintage/:get:operationId: getVintagesummary: Check the vintage of the winetags:- drinks- wineparameters:- name: drink_typein: pathdescription: The type of drinkrequired: trueschema:type: stringresponses:"200":description: the wine vintagecontent:application/json:schema:$ref: "#/components/schemas/Vintage"
The generated SDK will include methods that can be invoked as follows:
// Get the Vintagesdk.Drinks.GetVintage("wine")sdk.Wine.GetVintage("wine")