Customize responses
Response formats
When generating SDKs, response formats determine the structure of response types in supported languages. You can choose from three available response formats.
Configure the response format for a given target in the gen.yaml
file:
typescript: # Python and Go can be configured in a similar wayresponseFormat: flat # Or envelope-http, or envelopepackageName: @acme/super-sdkversion: 0.1.0author: SpeakeasytemplateVersion: v2clientServerStatusCodesAsErrors: truemaxMethodParams: 4flattenGlobalSecurity: trueinputModelSuffix: inputoutputModelSuffix: outputadditionalDependencies:dependencies: {}devDependencies: {}peerDependencies: {}imports:option: openapipaths:callbacks: models/callbackserrors: models/errorsoperations: models/operationsshared: models/componentswebhooks: models/webhooks
The following sections will reference this specification:
/payments/{id}:get:operationId: getPaymentparameters:- name: idin: pathrequired: trueschema:type: stringresponses:"200":description: Details about a paymentcontent:application/json:schema:$ref: "#/components/schemas/Payment"components:schemas:Payment:type: objectrequired: [id,amount,currency]properties:id:type: integeramount:type: numbercurrency:type: string
responseFormat: flat
The flat response format is the simplest and most ergonomic option, as it avoids generating a wrapper type, giving SDK users direct access to the response value.
When responseFormat: flat
is enabled, the generated SDK code will return the Payment
type directly with no indirection:
export class SDK extends ClientSDK {async getPayment(id: string, options?: RequestOptions): Promise<components.Payment> {}}
To debug HTTP metadata, users can pass a custom client to the SDK instance.
responseFormat: envelope-http
The envelope-http
format builds response types with a wrapper that holds the response value and associated HTTP metadata.
When envelope-http
is enabled, the generated SDK code will produce the response types below:
class SDK extends ClientSDK {async getPayment(id: string, options?: RequestOptions): Promise<operations.GetPaymentResponse> {}}
export type GetPaymentResponse = {httpMeta: components.HTTPMetadata;/*** Details about a payment*/payment?: components.Payment | undefined;};
export type HTTPMetadata = {/*** Raw HTTP response; suitable for custom response parsing*/response: Response;/*** Raw HTTP request; suitable for debugging*/request: Request;};
Built-in HTTP metadata is included in both custom and built-in error types that are thrown or returned from the SDK.
Of the three response formats, envelope-http
provides the most details about the underlying HTTP requests but adds a layer of indirection with a wrapper value.
responseFormat: envelope
The responseFormat: envelope
format builds response types with a wrapper that holds the response value and minimal information about the underlying HTTP response.
Using
envelope-http
instead ofenvelope
is recommended as it provides a more complete view of the HTTP request and response.
When responseFormat: envelope
is enabled, the generated SDK code will produce the response types below:
class SDK extends ClientSDK {async getPayment(id: string, options?: RequestOptions): Promise<operations.GetPaymentResponse> {}}
export type GetPaymentResponse = {/*** HTTP response content type for this operation*/contentType: string;/*** HTTP response status code for this operation*/statusCode: number;/*** Raw HTTP response; suitable for custom response parsing*/rawResponse: Response;/*** Details about a payment*/payment?: components.Payment | undefined;};