@speakeasy-api/speakeasy-client-sdk-typescript
SDK Installation
NPM
npm add @speakeasy-api/speakeasy-client-sdk-typescript
Yarn
yarn add @speakeasy-api/speakeasy-client-sdk-typescript
SDK Example Usage
Example
import { Speakeasy } from "@speakeasy-api/speakeasy-client-sdk-typescript";
async function run() {
const sdk = new Speakeasy({
security: {
apiKey: "<YOUR_API_KEY_HERE>",
},
workspaceID: "<value>",
});
const res = await sdk.apis.getApis({
metadata: {
key: ["<value>"],
},
op: {
and: false,
},
});
if (res.statusCode == 200) {
}
}
run();
Available Resources and Operations
Error Handling
Handling errors in this SDK should largely match your expectations. All operations return a response object or throw an error. If Error objects are specified in your OpenAPI Spec, the SDK will throw the appropriate Error type.
Example
import { Speakeasy } from "@speakeasy-api/speakeasy-client-sdk-typescript";
async function run() {
const sdk = new Speakeasy({
security: {
apiKey: "<YOUR_API_KEY_HERE>",
},
workspaceID: "<value>",
});
let res;
try {
res = await sdk.apis.deleteApi({
apiID: "<value>",
versionID: "<value>",
});
} catch (err) {
if (err instanceof errors.SDKError) {
console.error(err);
throw err;
}
}
if (res.statusCode == 200) {
}
}
run();
Server Selection
Select Server by Name
You can override the default server globally by passing a server name to the server: string
optional parameter when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the names associated with the available servers:
prod | https://api.prod.speakeasyapi.dev | None |
Example
import { Speakeasy } from "@speakeasy-api/speakeasy-client-sdk-typescript";
async function run() {
const sdk = new Speakeasy({
server: "prod",
security: {
apiKey: "<YOUR_API_KEY_HERE>",
},
workspaceID: "<value>",
});
const res = await sdk.apis.deleteApi({
apiID: "<value>",
versionID: "<value>",
});
if (res.statusCode == 200) {
}
}
run();
Override Server URL Per-Client
The default server can also be overridden globally by passing a URL to the serverURL: str
optional parameter when initializing the SDK client instance. For example:
import { Speakeasy } from "@speakeasy-api/speakeasy-client-sdk-typescript";
async function run() {
const sdk = new Speakeasy({
serverURL: "https://api.prod.speakeasyapi.dev",
security: {
apiKey: "<YOUR_API_KEY_HERE>",
},
workspaceID: "<value>",
});
const res = await sdk.apis.deleteApi({
apiID: "<value>",
versionID: "<value>",
});
if (res.statusCode == 200) {
}
}
run();
Custom HTTP Client
The Typescript SDK makes API calls using the axios HTTP library. In order to provide a convenient way to configure timeouts, cookies, proxies, custom headers, and other low-level configuration, you can initialize the SDK client with a custom AxiosInstance
object.
For example, you could specify a header for every request that your sdk makes as follows:
import { @speakeasy-api/speakeasy-client-sdk-typescript } from "Speakeasy";
import axios from "axios";
const httpClient = axios.create({
headers: {'x-custom-header': 'someValue'}
})
const sdk = new Speakeasy({defaultClient: httpClient});
Authentication
Per-Client Security Schemes
This SDK supports the following security schemes globally:
apiKey | apiKey | API key |
bearer | http | HTTP Bearer |
You can set the security parameters through the security
optional parameter when initializing the SDK client instance. The selected scheme will be used by default to authenticate with the API for all operations that support it. For example:
import { Speakeasy } from "@speakeasy-api/speakeasy-client-sdk-typescript";
async function run() {
const sdk = new Speakeasy({
security: {
apiKey: "<YOUR_API_KEY_HERE>",
},
workspaceID: "<value>",
});
const res = await sdk.apis.deleteApi({
apiID: "<value>",
versionID: "<value>",
});
if (res.statusCode == 200) {
}
}
run();
Global Parameters
A parameter is configured globally. This parameter may be set on the SDK client instance itself during initialization. When configured as an option during SDK initialization, This global value will be used as the default on the operations that use it. When such operations are called, there is a place in each to override the global value, if needed.
For example, you can set workspaceID
to "<value>"
at SDK initialization and then you do not have to pass the same value on calls to operations like getWorkspaceEvents
. But if you want to do so you may, which will locally override the global setting. See the example code below for a demonstration.
Available Globals
The following global parameter is available.
workspaceID | string | | The workspaceID parameter. |
Example
import { Speakeasy } from "@speakeasy-api/speakeasy-client-sdk-typescript";
async function run() {
const sdk = new Speakeasy({
security: {
apiKey: "<YOUR_API_KEY_HERE>",
},
workspaceID: "<value>",
});
const res = await sdk.events.getWorkspaceEvents({});
if (res.statusCode == 200) {
}
}
run();
Retries
Some of the endpoints in this SDK support retries. If you use the SDK without any configuration, it will fall back to the default retry strategy provided by the API. However, the default retry strategy can be overridden on a per-operation basis, or across the entire SDK.
To change the default retry strategy for a single API call, simply provide a retryConfig object to the call:
import { Speakeasy } from "@speakeasy-api/speakeasy-client-sdk-typescript";
async function run() {
const sdk = new Speakeasy({
security: {
apiKey: "<YOUR_API_KEY_HERE>",
},
workspaceID: "<value>",
});
const res = await sdk.auth.getWorkspaceAccess(
{},
{
strategy: "backoff",
backoff: {
initialInterval: 1,
maxInterval: 50,
exponent: 1.1,
maxElapsedTime: 100,
},
retryConnectionErrors: false,
}
);
if (res.statusCode == 200) {
}
}
run();
If you'd like to override the default retry strategy for all operations that support retries, you can provide a retryConfig at SDK initialization:
import { Speakeasy } from "@speakeasy-api/speakeasy-client-sdk-typescript";
async function run() {
const sdk = new Speakeasy({
retry_config: {
strategy: "backoff",
backoff: {
initialInterval: 1,
maxInterval: 50,
exponent: 1.1,
maxElapsedTime: 100,
},
retryConnectionErrors: false,
},
security: {
apiKey: "<YOUR_API_KEY_HERE>",
},
workspaceID: "<value>",
});
const res = await sdk.auth.getWorkspaceAccess({});
if (res.statusCode == 200) {
}
}
run();