Skip to content
FeedbackDashboard

Automate codegen with alternative workflows

Use a push-based workflow or poll your OpenAPI spec to trigger codegen after you make changes to your API

If you can’t use preview builds, we provide two alternative ways to automate codegen:

  • Push-based workflow: If you keep your OpenAPI spec in a GitHub or GitLab repository, but you don’t use pull/merge requests to make changes to it, use this workflow
  • Polling workflow: If your OpenAPI spec is available at a stable URL, use this workflow

If you make changes to your OpenAPI spec using direct commits in GitHub or GitLab without pull/merge requests, install our GitHub action to automate codegen.

1 Set up authentication for the workflow

Section titled “ Set up authentication for the workflow”

The GitHub workflow that powers preview builds requires authentication. We recommend you install the Stainless GitHub app to provide authentication via OIDC.

If you’re not using GitHub or can’t install the Stainless GitHub app, use an API key to authenticate instead.

To install the Stainless GitHub app:

  1. Open your Stainless dashboard
  2. If needed, navigate to the organization/project where you want to enable preview builds
  3. Go to the Release tab at the top and make sure Production repos is selected on the left
  4. Press the Install Stainless App button
  5. Follow the steps to complete the installation flow and give the Stainless app access to the repositories where you want to enable preview builds

Alternative authentication with an API key

If you don’t have admin permissions on GitHub, you’re using GitLab, or you’d prefer not to install the Stainless GitHub app, you can authenticate manually with an API key instead:

  1. Navigate to your organization’s settings page
  2. If you have more than one organization, make sure the correct one is selected
  3. Click Create new API key

Now add your API key as a secret so the workflow you add in the next step can use it:

See the next step and our example workflows for more details.

In the GitHub repository where you make changes to your OpenAPI spec, add the following .github/workflows/stainless-push.yml workflow file:

.github/workflows/stainless-push.yml
name: Build SDKs on push
on:
push:
branches:
- main
env:
# TODO: Stainless project name.
STAINLESS_PROJECT: YOUR_PROJECT
# TODO: Path to your OpenAPI spec.
OAS_PATH: YOUR_OAS_PATH
# Path to your Stainless config. Optional; only provide this if you prefer
# to maintain the ground truth Stainless config in your own repo.
CONFIG_PATH: YOUR_CONFIG_PATH
# The commit message to use for the SDK builds. Use a commit message in the
# conventional commits format: https://www.conventionalcommits.org/en/v1.0.0/
COMMIT_MESSAGE: "feat(api): update api"
# When to fail the job based on build conclusion.
# Options: "never" | "note" | "warning" | "error" | "fatal".
FAIL_ON: error
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Build SDK
uses: stainless-api/upload-openapi-spec-action/build@v1
with:
stainless_api_key: ${{ secrets.STAINLESS_API_KEY }}
org: ${{ env.STAINLESS_ORG }}
project: ${{ env.STAINLESS_PROJECT }}
oas_path: ${{ env.OAS_PATH }}

This workflow contains one job:

  • build - Runs when a commit is pushed to the branch you specify (default is main) to trigger codegen using the latest commit on that branch

Update the STAINLESS_PROJECT and OAS_PATH environment variables in this workflow (each one has a TODO comment) to match your project and OpenAPI spec file path, respectively.

This workflow requires the following GitHub permissions:

  • id-token: write for GitHub OIDC authentication
  • contents: read to check out your repository’s code to read your OpenAPI spec and config files

For more configuration details, see the workflow’s README file and our example workflow files.

If your OpenAPI spec is available at a stable URL, Stainless can poll that URL to detect API changes and trigger codegen.

This approach checks for updates hourly; if you want more responsive updates use the push-based workflow instead.

  1. In the Stainless dashboard, navigate to your project
  2. Go to Release > Setup OpenAPI spec sync > Fetch URL
  3. Enter the URL for your OpenAPI spec in the Spec URL field
  4. Press the Fetch Spec button

Once polling is set up, Stainless will look for changes to your OpenAPI spec every hour. If changes are found, codegen will be triggered.

You can tell Stainless to check for changes manually at any time by pressing the Fetch Spec button in your dashboard.