Skip to content

k.openApi ​

Invoke imported OpenAPI / Swagger endpoints and manage request authorization profiles

Execution ContextPrerequisitesTypical Use
Site API, CodeBlock, and server-side scriptsImport OpenAPI/Swagger spec under Develop → OpenAPITyped third-party REST API calls, switching credentials, and OAuth2 profiles

k.openApi parses OpenAPI / Swagger specifications configured in the Kooboo admin console and exposes strongly typed clients and operations directly in KScript, along with centralized credential management and response caching.

Quick Start ​

js
// Invoke a GET operation that takes only query parameters (e.g. GET /api/v1/categories)
const categories = k.openApi.Menoki._api_v1_categories_Get({
  limit: 2,
  offset: 0
})

// Select a saved authorization profile for this request when calling authenticated endpoints
k.openApi.PetStore.authorize.name = "sandbox"
const created = k.openApi.PetStore._pets_Post({ name: "doggie", status: "available" })

Quick Reference ​

Method / PropertyDescriptionReturns
k.openApi.<ServiceName>.<Operation>(...)Invoke a generated OpenAPI operation with positional parameter groupsResponse data any
<ServiceName>.authorize.nameRead or set the active credential profile name for the requeststring | null
<ServiceName>.authorize.list()List all saved authorization profile namesstring[]
<ServiceName>.authorize.addOrUpdate(name, securityKey, data)Create or update credentials for a profile and security keyvoid
<ServiceName>.authorize.delete(name)Delete a named authorization profilevoid
<ServiceName>.authorize.addOrUpdate_<securityKey>(name, data)Security-scheme-specific credential helpervoid
<ServiceName>.authorize.getAuthorizationUrl_<securityKey>(name, clientId, clientSecret)Generate an OAuth2 Authorization Code redirect URLstring

Configure in Admin First

Import an OpenAPI specification or configure its URL under Development → OpenAPI before calling it in scripts. See OpenAPI Docs for setup and cache rules.

Access Structure and Method Naming ​

Access each configured OpenAPI client by its saved service name:

js
k.openApi.<ServiceName>.<OperationName>(...)

Operation method names combine the normalized path with the title-cased HTTP method:

  1. Non-word characters in the path (including the leading /, slashes, hyphens, and braces) are replaced with _ (so paths starting with / start with _).
  2. If the normalized name starts with a digit, an extra leading _ is prepended.
  3. _<Method> is appended at the end (such as _Get, _Post, _Put, _Delete).
OpenAPI Path & MethodGenerated Method Name
GET /pets_pets_Get
POST /pets_pets_Post
GET /pets/{petId}_pets__petId__Get
GET /api/v1/categories_api_v1_categories_Get

Calling Operations and Positional Parameters ​

Generated OpenAPI operations accept arguments in a fixed positional order, where only parameter categories declared by that operation occupy argument slots:

js
operation(body, query, path, header, cookie)
  • Pass each category object positionally—do not wrap arguments in an extra { query: { ... } } or { body: { ... } } envelope.
  • When an operation defines only query parameters (and no requestBody), the first argument is the query object itself.
js
// 1. GET operation with query parameters only (e.g. GET /api/v1/categories)
const categories = k.openApi.Menoki._api_v1_categories_Get({
  limit: 2,
  offset: 0
})

// 2. GET operation with both query and path parameters
// Assuming GET /pets/{petId} declares query param `expand` and path param `petId`:
const pet = k.openApi.PetStore._pets__petId__Get({ expand: true }, { petId: "123" })

// 3. POST operation with a request body
const created = k.openApi.PetStore._pets_Post({
  name: "doggie",
  status: "available"
})

Authorization Management (authorize) ​

Every OpenAPI client provides authorize (k.openApi.<ServiceName>.authorize) to select an active credential profile per request or create/remove stored credentials via script (full typed definitions are generated when components.securitySchemes is declared):

MemberType / SignatureDescription
namestring | nullActive authorization profile name for the current request (defaults to null, falling back to the first configured profile)
list()() => string[]Lists all saved authorization profile names for this OpenAPI client
addOrUpdate(name, securityKey, data)(name, securityKey, data) => voidCreates or updates credentials for a profile name and security scheme key
delete(name)(name) => voidDeletes the named authorization profile

When the OpenAPI spec declares components.securitySchemes, typed helper methods are also generated per <securityKey>:

  • addOrUpdate_<securityKey>(name, data) (returns void)
  • getAuthorizationUrl_<securityKey>(name, clientId, clientSecret) (returns string, for OAuth2 Authorization Code redirects)

Supported fields in SecurityData:

Case-sensitive PascalCase keys at runtime

Although Kooboo's generated TypeScript definitions declare camelCase keys (and expiresIn?: string), at runtime addOrUpdate and addOrUpdate_<securityKey> perform case-sensitive PascalCase lookups (Username, Password, ClientId, ClientSecret, AccessToken, RefreshToken, ExpiresIn, RedirectUrl, Name), and ExpiresIn must be a JS Date object (a string is ignored). Passing camelCase keys silently stores null/default values.

Field (Runtime Key)TypeDescription
UsernamestringHTTP Basic authentication username (typed as username in generated TS)
PasswordstringHTTP Basic authentication password (typed as password in generated TS)
ClientIdstringOAuth2 client_id (typed as clientId in generated TS)
ClientSecretstringOAuth2 client_secret (typed as clientSecret in generated TS)
AccessTokenstringBearer token, API key token, or OAuth2 access token (typed as accessToken in generated TS)
RefreshTokenstringOAuth2 refresh token (typed as refreshToken in generated TS)
ExpiresInDateOAuth2 access token UTC expiration timestamp; must be a JS Date object at runtime (typed as expiresIn?: string in generated TS)
RedirectUrlstringOAuth2 callback redirect URL
NamestringAuthorization header token prefix (defaults to "Bearer") for HTTP Bearer and OAuth2 schemes; not used for API key header/query names (which always come from securityScheme.name in the OpenAPI spec)
js
const api = k.openApi.PetStore

// List configured authorization profiles
const names = api.authorize.list()

// Select which authorization profile to use for subsequent calls in this request
api.authorize.name = "sandbox"

// Add or update credentials (use PascalCase keys and a Date object for ExpiresIn at runtime)
api.authorize.addOrUpdate("sandbox", "bearer_auth", {
  AccessToken: "your-token-here",
  Name: "Bearer",
  ExpiresIn: new Date(Date.now() + 3600 * 1000)
})

For details on configuring security schemes in the admin console, see OpenAPI Authorization Config.

Response Cache ​

Response caching can be configured in the Kooboo admin console by path substring match (Pattern, matched via Contains), HTTP method, and expiration duration without custom cache boilerplate in scripts; see OpenAPI Common Fields.

After saving an OpenAPI specification in the admin console, Kooboo automatically generates TypeScript definitions from paths, parameters, requestBody, responses, schemas, and securitySchemes to provide parameter and response autocomplete.

TypeScript Definitions ​

Show full TypeScript definitions
ts
declare namespace OpenApi {
  interface openApi {
    // Each imported OpenAPI specification generates a matching client property
    [serviceName: string]: any;
  }
}

// Note: Generated TS definitions emit camelCase (and `expiresIn?: string`),
// but runtime KAuthorize.AddOrUpdate is case-sensitive and reads the PascalCase keys below (with `Date` for `ExpiresIn`).
interface SecurityData {
  Username?: string;
  Password?: string;
  ClientId?: string;
  ClientSecret?: string;
  AccessToken?: string;
  RefreshToken?: string;
  ExpiresIn?: Date;
  RedirectUrl?: string;
  Name?: string;
}

interface HttpBasicSecurityData {
  Username: string;
  Password: string;
}

interface TokenSecurityData {
  AccessToken: string;
  Name?: string;
}

interface OAuth2SecurityData {
  ClientId: string;
  ClientSecret: string;
  AccessToken?: string;
  RefreshToken?: string;
  ExpiresIn?: Date;
  RedirectUrl?: string;
}

interface KOpenApiAuthorize {
  name: string;
  list(): string[];
  addOrUpdate(name: string, securityKey: string, data: SecurityData): void;
  delete(name: string): void;
}