This site counts visits with analytics cookies only if you accept. Read the consent

Skip to content

StudioImport an API

Import an API

Turn an OpenAPI 3, Swagger 2 or Postman specification into typed API code, and keep it up to date as the specification changes.

11 min readUpdated both modes

Importing

Import API on the APIs page turns an OpenAPI 3, Swagger 2 or Postman specification — from a URL, a file, or pasted text — into typed API code: pick the endpoints, choose the options, preview the files, then write them. Studio generates the files itself and writes them to the output directory, in a folder named after the source in kebab case (src/api/pets/ for pets, src/api/pet-store/ for petStore), where source.ts exports the source, and records what it wrote in .courier/imports-state.json — see Which files Studio changes.

Studio lists only the sources courier.config.ts declares — it does not look for API files in the project. So after the write the wizard shows the change that declares the import there — the import of the generated source, its sources entry and its imports entry — and Add to courier.config.ts makes it: Studio adds those lines and nothing else, written like the rest of the file, loads the config, and the API appears on the APIs page, in the Explorer and in the Playground. A config saved after the change was shown is never overwritten — the wizard shows the change against it as it is now — and one that does not load once changed is put back as it was. A config that only passes another module's default export on — export { default } from "./src/courier/config", as a server whose TypeScript compiles src/ alone has it — is changed where its object is written, in that module, so the server registers the import too. When the config is written in a way Studio cannot add to safely, such as sources built by a function call, or the browser is not on the machine Studio runs on, the wizard shows the lines to add by hand and says why. Either way the config ends up like this:

ts
// courier.config.ts
import { defineConfig } from "@noverax/courier/config"
import { api } from "./src/api"
import { pets } from "./src/api/pets/source"

export default defineConfig({
  sources: { api, pets },
  imports: {
    pets: {
      importType: "url",
      type: "openapi",
      from: "https://specs.example.com/pets.json",
      outputDir: "src/api",
      sourceName: "pets",
    },
  },
})

A config that keeps its list of APIs in a module of the project — import { sources } from "./lib/sources", by a relative path or a tsconfig path alias, the module exporting the object as export const sources = { … }, as a default export, or under another name it imports by — is changed in both files as one change. The generated source is imported and listed in that module, the imports entry goes in the config, the wizard shows the lines each file gains, and the button reads Add to sources.ts and courier.config.ts. Studio writes both files or neither: a save to either after the change was shown changes neither, and when the config does not load once changed, both are put back as they were. A module that builds the list by a function call, or that is a package's rather than the project's, is left to you with the lines to add, each under the file it goes in:

ts
// lib/sources.ts
import { api } from "../src/api"
import { pets } from "../src/api/pets/source"

export const sources = { api, pets }
ts
// courier.config.ts
import { defineConfig } from "@noverax/courier/config"
import { sources } from "./lib/sources"

export default defineConfig({
  sources,
  imports: {
    pets: {
      importType: "url",
      type: "openapi",
      from: "https://specs.example.com/pets.json",
      outputDir: "src/api",
      sourceName: "pets",
    },
  },
})
  • sources is what Studio lists APIs from, and what your app calls. An API written by hand is declared there the same way. Studio matches an import to the source of its name, so an import needs a name no other source takes: while sources declares another API under it, Studio writes nothing and says so. The APIs page lists a source as imported only when the config loads the source.ts Studio generated for it.
  • imports is what Studio checks and manages an import from: the update check fetches from again, and when the specification changed, Review update — Manage given a newer specification — updates the import to it: every endpoint you keep is generated from it afresh, so its types and schemas follow the specification, while an endpoint you edited keeps your code; you tick the new endpoints to add and untick the ones to drop; the endpoints the specification no longer has go, and Studio keeps a copy of every file it changes. The endpoints you add are generated with the options written there, and the ones you left out when you imported stay out. With the same specification and nothing picked, Manage offers Regenerate files: every file written again as the running version of Studio writes it, your edits to endpoints kept — the way to bring an import written by an older Studio up to date. The wizard's entry carries the source name and every option you changed from its default, but not the endpoints you left out in the Select step — the entry has no way to keep that selection — so Manage lists those as endpoints you can add, and the wizard says how many there are. A file or pasted import has no from: Manage asks for its specification each time, and the update check marks it not checked, rather than up to date. Studio keeps each import's last check while it runs, and every page shows it, one opened or reloaded later too; a check that fails says why on the import's card. The update check sends auth with the request to from's own origin, never to a host it redirects to, and refuses a specification whose hash is not the one pinHash names — the hash Studio records for the import as specHash in .courier/imports-state.json. An update, Manage and a new write of a declared import generate from what its transform returns for the parsed specification, keeping only the endpoints include names, by tag, by path as the specification writes it, or by operationId, and then leaving out those exclude names.
  • Importing the same name again keeps the endpoints you edited by hand, while the specification still describes them, and the ones you added, in the files that declare them. Every other generated file — the source of an API grouped by tag or path, its index.ts, the schemas — is written as generated; when it holds edits of yours, Studio asks before replacing it. A group left out this time has its files removed, as long as Studio wrote them and nobody changed them; the message after the write says how many files it removed, and their copies are kept.
  • A part the config declares already is left out, and once it declares both, the wizard closes after the write and says only what it wrote.

How each part of a specification becomes code — the groups and their names, each operation and its parameters, the schemas and what they allow, and the folder an import writes — is on How a Specification Becomes Code.

What an import writes into the project — the generated files and courier.config.ts — the copies Studio keeps of what it replaces, how a change Studio was stopped in the middle of is undone, and how Studio formats what it writes are on Files Studio Writes.

From the command line

courier-studio import imports a specification as Import API does — the same files, written into the same folder and recorded the same way — from a terminal, a script or a CI job, without Studio running:

bash
npx @noverax/courier-studio import https://petstore3.swagger.io/api/v3/openapi.json

It takes a URL, a file, or - for standard input, and names the import after the specification, as the wizard does, unless --name names it. It shows the plan first — every file it creates, updates, replaces or removes, and the lines that declare the import in courier.config.ts — and asks before it writes anything. Then it declares the import as Add to courier.config.ts does: the lines added, written like the rest of the file — in the module the config takes its sources from too, when it does — the config loaded, and every file put back as it was when it no longer loads.

  • --yes writes without asking, taking the plan as the wizard offers it: a step that waits for you — replacing a file you changed, say — is taken only when the wizard offers it ticked, or when --confirm names its path. Every other file stays as it is, and the command says how to allow it. Where no one can answer, as in a CI job, nothing is written without --yes.
  • --dry-run shows the plan and writes nothing.
  • --include and --exclude pick endpoints by tag, by path as the specification writes it, or by operationId — --include tag:pets,op:getUser — and the import's imports entry records them, so an update keeps the selection. Once the config declares the import, a selector other than the one it declares is refused: change it in the config.
  • --out, --validation, --group-by, --client, --no-types, --no-schemas, --no-comments and --auth-middleware are the wizard's options, with its defaults.
  • --no-declare writes the files only, and prints the lines to add to courier.config.ts — each under the file it goes in, when the config takes its sources from a module.
  • A URL is fetched by the wizard's rules — see Specifications on a private network. A URL is whatever a URL parser reads as an http:// or https:// address once trimmed, a space a paste left before it or \\ for // included, so a user and a password in it are refused as Studio refuses them; a file's path the parser reads as another kind of address, ftp://…, is not repeated in what the command says of it.
  • --bearer-env ORDERS_SPEC_TOKEN, --basic-env SPEC_USER:SPEC_PASSWORD or --header-env "X-Api-Key=ORDERS_API_KEY,X-Tenant=TENANT_ID" name the environment variables a specification behind auth is read with, one kind at a time, and the import's entry records the same names — see Specifications behind auth. The command takes no credential itself, which would stay in the shell's history.
  • The project needs a courier.config.ts: without one, the command prints the config to start from. Its variables are read first, as Studio reads them — --dotenv names the files.

It exits 0 once the import is written and declared, or its plan shown with --dry-run, and 1 when it writes nothing or something fails.

From traffic, for an API with no specification

An API with no specification can be declared from the calls your app makes to it. A source the app defines in its own code, which courier.config.ts does not list, shows in the Explorer with what its traffic tells — and its endpoints' panels say they are known only by their traffic. Declare <source> from traffic reads an OpenAPI 3.1 document from the calls Studio recorded and opens Import API on it, as though it were pasted; from there it is imported as any specification is: pick the endpoints, choose the options, preview the files, write them, and the source is declared.

  • Routes. A segment every call shares is written as it is; one that differs between calls, or that was a variable's value, is a parameter named after that variable — /products/{id} — or param1 when no variable carried it. The segments every endpoint's paths start with are the API's base path.
  • Requests. A read's query is the keys its calls sent, required when every call sent them, typed by the variables they carried; a write's body is its variables less those in its path.
  • Answers. The answer's schema is merged from every answer that came whole with the status most calls got: a field is required when every answer held it, nullable when it came as null, of every type it came as; a string has a format — date-time, uuid, email — when every string there had it. Numbers stay numbers: a few answers cannot tell a price of 12 from an integer. No value goes into the document.
  • Names. An endpoint keeps the name and the group the app gave it, as its operation id and tag. An answer that is a list of objects names the schema of one of them after its endpoint — ProductsListItem for products.list — so the code has a type for one item rather than an unnamed one nested in the list.
  • How sure each part is. Every field, path parameter and query parameter read from traffic says so, and on how many calls it rests: Inferred from traffic: in 2 of 3 answers., Inferred from traffic: sent in all 4 calls. The generated code keeps it as the field's comment, so whoever reads the code sees which parts rest on one call and which on many.
  • What is left out. A stream or a channel, a call with no answer, and an answer kept only in part; an endpoint whose route and method another already took is named rather than written twice.

The document is only as complete as the traffic: an endpoint the app never called is not in it, and a field no answer held is not in its schema. Call what you want declared first — or declare it now and follow its contract as traffic shows more.

A source declared this way stays tied to its traffic. The APIs page says it was read from traffic, and Manage endpoints reads the calls Studio recorded again rather than asking for a specification: an endpoint the app has called since is there to add, and one the traffic no longer shows stays until you remove it.

An endpoint of a declared source can show up in traffic too, when the app calls one the declaration does not have; its panel says it was called, but not declared. For a source declared from its traffic — while the app still calls its own definition — the panel offers Add <endpoint> from traffic, which opens Manage endpoints on the source with that endpoint picked, and the change is shown before it is written. For any other source it means the app runs another version of it than the one Studio read — one built before a change to its definition, or a definition the config does not import — so restart the app, or point the config at the definition the app uses.

Private and protected specifications

A specification on a private network, which the wizard reaches only as you allow, and one behind auth, whose credentials Studio reads from environment variables, are on Private Specifications.