SDKs
Customize methods
Change method names
Section titled “Change method names”Speakeasy uses your OpenAPI schema to infer names for class types, methods, and parameters. However, you can override these names to tailor the generated SDK to your preferences.
The x-speakeasy-name-override extension can be used to override the name of a class, method, or parameter. Depending on where this extension is placed in an OpenAPI schema, names can be overridden at different scopes, such as globally or for specific operations and parameters.
For example, the x-speakeasy-name-override extension can be used to override the generated name for a method generated from an operation.
This extension can be applied globally by placing it at the root of the OpenAPI schema, allowing all methods with an operationId that matches the provided operationId regex to be overridden with methodNameOverride.
openapi: 3.1.0info: title: Test version: 0.0.1servers: - url: https://httpbin.orgsecurity: - basicAuth: []x-speakeasy-name-override: - operationId: ^get.* methodNameOverride: get - operationId: ^post.* methodNameOverride: createpaths: /test: get: operationId: getTest responses: "200": description: OK post: operationId: postTest requestBody: required: true content: application/json: schema: $ref: "#/components/schemas/Test" responses: "200": description: OKSince getTest and postTest match the ^get.* and ^post.* regexes defined by the global x-speakeasy-name-override extension, these method names will be generated as get and create, respectively.
Alternatively, x-speakeasy-name-override can be used at the operation level to override the generated method name for that specific operation only. If the OpenAPI schema includes the extension at both the global and operation levels, the operation-level extension will take precedence.
Consider the same schema shown above with an operation-level extension added to the get operation:
---get: operationId: getTest x-speakeasy-name-override: getRandomTest responses: "200": description: OKNow, postTest will be generated as create as before, but getTest will be generated as getRandomTest.
Configuring method signatures
Section titled “Configuring method signatures”To customize method signatures in an SDK, control how parameters are passed to the method by setting the maxMethodParams configuration option in the gen.yaml file.
Here is an example of how to set the maxMethodParams configuration option in your gen.yaml file:
configVersion: 2.0.0generation: # ...typescript: maxMethodParams: 4 # ...Here, the maxMethodParams configuration option is set to 4, so the generated SDK will have a maximum of four parameters for each method, including the request body parameter.
If the number of parameters for a method exceeds the maxMethodParams configuration option, the generated SDK will use a single request object parameter to encapsulate all the parameters.
To ensure the generator always creates a request object for an SDK, set maxMethodParams to 0. This approach is useful for enabling request objects to evolve gracefully, avoiding breaking changes to the method signature when adding parameters in the future.
Here are examples of an SDK with maxMethodParams set to 4 and 0:
// Example of SDK with maxMethodParams set to 4import { Speakeasybar } from "speakeasy"; async function run() {const sdk = new Speakeasybar(); const ingredient = "vodka"; const name = "martini"; const limit = 100; const result = await sdk.drinks.listDrinks(ingredient, name, limit); // Handle the result console.log(result); } run(); // Example of SDK with maxMethodParams set to 0import { Speakeasybar } from "speakeasy"; async function run() {const sdk = new Speakeasybar(); const result = await sdk.drinks.listDrinks({ ingredient: "vodka", name: "martini", limit: 100, }); // Handle the result console.log(result); } run();# Example of SDK with maxMethodParams set to 4import sdk s = sdk.SDK() res = s.drinks.list_drinks(ingredient='vodka', name='martini', limit=100) if res.drinks is not None: # handle response pass # Example of SDK with maxMethodParams set to 0import sdkfrom sdk.models import operations s = sdk.SDK() req = operations.ListDrinksRequest( ingredient='vodka', name='martini', limit=100,) res = s.drinks.list_drinks(req) if res.drinks is not None: # handle response pass// Example of SDK with maxMethodParams set to 4package main import ( "context" "log" "speakeasy") func main() {s := speakeasy.New() var ingredient *string = speakeasy.String("vodka") var name *string = speakeasy.String("martini") var limit *int64 = speakeasy.Int64(100) ctx := context.Background() res, err := s.Drinks.ListDrinks(ctx, ingredient, name, limit) if err != nil { log.Fatal(err) } if res.Drinks != nil { // handle response } } // Example of SDK with maxMethodParams set to 0package main import ( "context" "log" "speakeasy" "speakeasy/models/operations") func main() {s := speakeasy.New() ctx := context.Background() res, err := s.Drinks.ListDrinks(ctx, operations.ListDrinksRequest{ Ingredient: speakeasy.String("vodka"), Name: speakeasy.String("martini"), Limit: speakeasy.Int64(100), }) if err != nil { log.Fatal(err) } if res.Drinks != nil { // handle response } }// Example of SDK with maxMethodParams set to 4package hello.world; import java.math.BigDecimal;import java.math.BigInteger;import java.time.LocalDate;import java.time.OffsetDateTime;import java.util.Optional;import org.openapis.openapi.SDK;import org.openapis.openapi.models.components.*;import org.openapis.openapi.models.operations.*;import org.openapis.openapi.models.operations.ListDrinksRequest;import org.openapis.openapi.models.operations.ListDrinksResponse;import static java.util.Map.entry; public class Application { public static void main(String[] args) { try { SDK sdk = SDK.builder() .build(); ListDrinksResponse res = sdk.drinks().listDrinks() .ingredient("vodka") .name("martini") .limit(100L) .call(); if (res.drinks().isPresent()) { // handle response } } catch (org.openapis.openapi.models.errors.SDKError e) { // handle exception } catch (Exception e) { // handle exception } }} // Example of SDK with maxMethodParams set to 0package hello.world; import java.math.BigDecimal;import java.math.BigInteger;import java.time.LocalDate;import java.time.OffsetDateTime;import java.util.Optional;import org.openapis.openapi.SDK;import org.openapis.openapi.models.components.*;import org.openapis.openapi.models.operations.*;import org.openapis.openapi.models.operations.ListDrinksRequest;import org.openapis.openapi.models.operations.ListDrinksResponse;import static java.util.Map.entry; public class Application { public static void main(String[] args) { try { SDK sdk = SDK.builder() .build(); ListDrinksRequest req = ListDrinksRequest.builder() .ingredient("vodka") .name("martini") .limit(100L) .build(); ListDrinksResponse res = sdk.drinks().listDrinks() .request(req) .call(); if (res.drinks().isPresent()) { // handle response } } catch (org.openapis.openapi.models.errors.SDKError e) { // handle exception } catch (Exception e) { // handle exception } }}// Example of SDK with maxMethodParams set to 4using Openapi;using Openapi.Models.Operations;using Openapi.Models.Shared;using NodaTime; var sdk = new SDK(); var res = await sdk.Drinks.ListDrinksAsync("vodka", "martini", 100); // handle response // Example of SDK with maxMethodParams set to 0using Openapi;using Openapi.Models.Operations;using Openapi.Models.Shared;using NodaTime; var sdk = new SDK(); var req = new ListDrinksRequest(){Ingredient = "vodka",Name = "martini",Limit = 100}; var res = await sdk.Drinks.ListDrinksAsync(req); // handle response// Example of SDK with maxMethodParams set to 4declare(strict_types=1); require 'vendor/autoload.php'; use OpenAPI\OpenAPI; $sdk = OpenAPI\SDK::builder()->build(); $response = $sdk->drinks->listDrinks("vodka", "martini", 100); // handle response // Example of SDK with maxMethodParams set to 0declare(strict_types=1); require 'vendor/autoload.php'; use OpenAPI\OpenAPI; $sdk = OpenAPI\SDK::builder()->build(); $request = new Shared\ListDrinksRequest(ingredient="vodka", name="martini", limit=100); $response = $sdk->drinks->listDrinks($request); // handle response# Example of SDK with maxMethodParams set to 4require 'openapi' Models = ::OpenApiSDK::Models s = ::OpenApiSDK::SDK.new response = s.drinks.list_drinks("vodka", "martini", 100) # handle response # Example of SDK with maxMethodParams set to 0require 'openapi' Models = ::OpenApiSDK::Models s = ::OpenApiSDK::SDK.new request = Models::Shared::ListDrinksRequest.new( ingredient: "vodka", name: "martini", limit: 100) response = s.drinks.list_drinks(request) # handle response You can also set maxMethodParams using the x-speakeasy-max-method-params extension in your OpenAPI document, either globally at the root of the document or at the operation level.
The order of precedence for configuration is:
- Operation-level
x-speakeasy-max-method-params - Global-level
x-speakeasy-max-method-params - The
maxMethodParamsconfiguration option in thegen.yamlfile
The configuration set in gen.yaml or through the extension at the root of the document will apply to all operations unless an operation-level extension overrides it.
Exclude parameters from signatures
Section titled “Exclude parameters from signatures”To exclude certain parameters from the generated SDK, use the x-speakeasy-ignore extension.
The following example uses x-speakeasy-ignore: true to exclude a parameter:
paths: /test/user/{user_id}: parameters: - name: user_id in: path required: true schema: type: string - name: status x-speakeasy-ignore: true in: query required: true schema: type: string get: operationId: getUser responses: "200": description: OK ...Exclude methods from an SDK
Section titled “Exclude methods from an SDK”Use the x-speakeasy-ignore extension to exclude certain methods from the generated SDK.
The following example illustrates several instances of x-speakeasy-ignore: true used across a schema.
paths: /test: get: x-speakeasy-ignore: true operationId: getTest responses: "200": description: OK