# Triggering and aborting builds

| Endpoints | Function | Required role on the app's team |
| --- | --- | --- |
| POST /apps/\{app-slug\}/builds | Trigger a new build. | Owner, Admin, or Developer |
| POST /apps/\{app-slug\}/builds/\{build-slug\}/abort | Abort a specific build. | Owner, Admin, or Developer |

You can trigger and abort builds with the Bitrise API. Define parameters for the build: for example, branch, tag or git commit to use. Custom [Environment Variables](/bitrise-ci/configure-builds/environment-variables) can be defined as well.

## Triggering a new build with the API

To trigger a new build with the Bitrise API, call the `POST /apps/{APP-SLUG}/builds` endpoint. You need to specify an app slug and at least one build parameter in a JSON object:

- A git tag or git commit hash
- A branch
- A Workflow ID or a Pipeline ID

:::note[Required role]

You must have a developer, admin, or owner role on the app's team to trigger a new build using the API.

For a complete list of user roles and role cheatsheets, check [Roles and permissions for Bitrise CI](/bitrise-platform/projects/roles-and-permissions-for-bitrise-ci).

:::

The JSON object must also contain a `hook_info` object with a `type` key and `bitrise` as the value of the key.

Here’s a minimal sample JSON body which specifies `main` as the value of the `branch` parameter:

```json
{
  "hook_info": {
    "type": "bitrise"
  },
  "build_params": {
    "branch": "main"
  }
}
```

And here’s the curl request syntax for triggering a build with the `prod` Workflow on the `main` branch:

```bash
curl -X 'POST' \
  'https://api.bitrise.io/v0.1/apps/APP-SLUG/builds' \
  -H 'accept: application/json' \
  -H 'Authorization: ACCESS-TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
  "build_params": {
    "branch": "main",
    "workflow_id":"prod"
  },
  "hook_info": {
    "type": "bitrise"
  }
}'
```

:::tip[Interactive cURL call configurator]

You can find an interactive cURL call configurator by clicking on the `Start/Schedule a build` button on your app’s [bitrise.io](https://www.bitrise.io) page and switching to `Advanced` mode in the popup. At the bottom of the popup you can find a `curl` call, based on the parameters you specify in the popup.

:::

In the previous example, we passed this JSON payload as a string: to be precise, as a JSON object serialized to a string.

You can also pass it as an object (for example, if you want to call it from JavaScript). To do so, include a root `payload` element or, alternatively, set the JSON object as the value of the `payload` POST parameter.

Here’s a jQuery example using the `payload` parameter:

```javascript
$.post("https://api.bitrise.io/v0.1/apps/APP-SLUG/builds/", {
    "payload":{
        "hook_info":{
            "type":"bitrise"
        },
        "build_params":{
            "branch":"main",
            "workflow_id":"prod"
        }
    }
})
```

You can specify several different build parameters when triggering a build. The parameters should be set in the `build_params` object.

### Setting a branch, commit or tag to build

You can set Git-specific parameters in your call. The `branch` parameter specifies the source branch to be built. This is either the branch of the git commit or, in the case of a pull request build, the source branch of the pull request.

```bash
curl -X 'POST' \
  'https://api.bitrise.io/v0.1/apps/APP-SLUG/builds' \
  -H 'accept: application/json' \
  -H 'Authorization: ACCESS-TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
	"hook_info": {
		"type": "bitrise"
	},
	"build_params": {
		"branch": "main",
                "workflow_id":"prod"
	}
 }'
```

You can also build a specific git commit or even a git tag: you just need to set either the commit hash or the tag in the `build_params` object. You can also set a commit message for the build with the `commit_message` parameter.

```bash
curl -X 'POST' \
  'https://api.bitrise.io/v0.1/apps/APP-SLUG/builds' \
  -H 'accept: application/json' \
  -H 'Authorization: ACCESS-TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
  "hook_info":{
    "type":"bitrise"
  },
  "build_params": {
    "commit_hash": "0000ffffeeeee",
    "commit_message": "testing",
    "workflow_id":"prod"
  }
}'
```

:::important[Parameter priority]

The `Git Clone` Step has the following parameter priority:

1. commit_hash
1. tag
1. branch

If you provide multiple parameters, the parameter with lower priority will be ignored.

The ignored parameters will still be logged. They will be available for Steps and they will be visible on the Build’s **Details** page but the `Git Clone` Step will use the most specific parameter for checkout.

:::

### Setting parameters for pull request builds

For a pull request build, use the `branch_dest` parameter to set up the destination or target branch of the pull request. The PR will be merged into this branch but before that, Bitrise will build your app based on how the code would look like after merging. This is what happens when a PR build is automatically triggered by a webhook, for example.

:::note[GitHub stacked pull requests]

When a webhook triggers a build for a pull request in a [GitHub stack](https://docs.github.com/en/pull-requests/get-started/about-stacked-prs), Bitrise sets `branch_dest` to the base branch of the stack.

:::

The `branch_repo_owner` and `branch_dest_repo_owner` parameters are used to identify the owners of the repositories, to unambiguously identify the branches involved in the pull request.

:::important[Pull request builds from forks]

If you do not specify the `branch_repo_owner` and `branch_dest_repo_owner` parameters, the API will assume pull request builds are coming from a fork. As such, they might be put on hold pending manual approval: [Approving Pull Request builds](/bitrise-ci/run-and-analyze-builds/starting-builds/approving-pull-request-builds).

:::

To identify the PR itself, use the `pull_request_id` parameter: it takes an integer; for example, the number of the PR on GitHub.

```bash
curl -X 'POST' \
  'https://api.bitrise.io/v0.1/apps/APP-SLUG/builds' \
  -H 'accept: application/json' \
  -H 'Authorization: ACCESS-TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
  "hook_info":{
    "type":"bitrise"
  },
  "build_params": {
    "branch": "the-pr-branch",
    "branch_dest": "main",
    "commit_hash": "fffff000000eeeeee",
    "pull_request_id": 1,
    "workflow_id":"prod"
  }
}'
```

If your git provider supports it, you can also use the `pull_request_merge_branch` parameter to build the pre-merged state of the branch of the PR. Another alternative is the `pull_request_head_branch` parameter: this is a special git ref that should point to the source of the PR.

If you want to trigger a build from a PR opened from a fork of your repository, use the `pull_request_repository_url` parameter. The value should be the URL of the fork.

### Skipping Git status reports

If you have a [webhook](/bitrise-platform/integrations/webhooks/adding-incoming-webhooks) set up, Bitrise will send status reports to your git provider about your builds. However, this can be disabled via the API: use the `skip_git_status_report` parameter. If it is set to `true`, no build status report will be sent.

```bash
curl -X 'POST' \
  'https://api.bitrise.io/v0.1/apps/APP-SLUG/builds' \
  -H 'accept: application/json' \
  -H 'Authorization: ACCESS-TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
  "hook_info":{
    "type":"bitrise"
  },
  "build_params": {
    "branch": "the-pr-branch",
    "branch_dest": "main",
    "skip_git_status_report": true,
    "workflow_id":"prod"
  }
}'
```

### Specifying Environment Variables

You can define additional [Environment Variables](/bitrise-ci/configure-builds/environment-variables) (Env Vars) for your build. These additional variables will be handled with priority between `Secrets` and `App Env Vars`, which means that you can not overwrite Env Vars defined in your build configuration (for example, App Env Vars), only [Secrets](/bitrise-ci/configure-builds/secrets).

Define additional Env Vars with the environments parameter. This parameter must be an **array of objects**, and every item of the array must include at least a `key` property. This must contain:

- The key of the Env Var.
- The value of the Env Var.

:::note[Replacing Env Var names]

By default, Env Var names inside values will be replaced in triggered build by actual value from the target environment. This behavior can be disabled by setting the `is_expand` flag to `false`.

:::

```yaml
"environments":[
  {"key":"API_TEST_ENV","value":"This is the test value","is_expand":true},
  {"key":"HELP_ENV","value":"$HOME variable contains user's home directory path","is_expand":false},
]
```

### Setting a Workflow for the build

By default, the Workflow for a triggered build will be selected based on the content of `build_params` and your app’s [trigger map](/bitrise-ci/run-and-analyze-builds/build-triggers/yaml-syntax-for-build-triggers). This is the same as how webhooks select the Workflow for the build automatically, based on the Trigger Map.

With the API, you can overwrite this selection and specify exactly which Workflow you want to use.

Add a `workflow_id` parameter to your `build_params` and specify the workflow you want to use for that specific build. Here’s an example call where we specify the `deploy` workflow:

```bash
curl -X 'POST' \
  'https://api.bitrise.io/v0.1/apps/APP-SLUG/builds' \
  -H 'accept: application/json' \
  -H 'Authorization: ACCESS-TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
    "hook_info": {
        "type": "bitrise"
    },
    "build_params": {
        "branch": "main",
        "workflow_id": "deploy"
    }
}'
```

## Aborting a build

You can abort running builds, and set the reason for aborting, as well as specify if email notifications should be sent about the build.

:::note[Required role]

You must have an owner, admin, or developer role on the app's team to abort a build using the API.

For a complete list of user roles and role cheatsheets, check [Roles and permissions for Bitrise CI](/bitrise-platform/projects/roles-and-permissions-for-bitrise-ci).

:::

To simply abort the build, call the `/apps/APP-SLUG/builds/BUILD-SLUG/abort` endpoint. It requires three parameters:

- The app slug.
- The build slug.
- The build abort parameters body. This can be left empty if you don't want to set any specific parameters for the abort: `-d "{}"`.

```bash
curl -X POST -H "Authorization: ACCESS-TOKEN" "https://api.bitrise.io/v0.1/apps/APP-SLUG/builds/BUILD-SLUG/abort" -d "{}"
```

You can set a reason for aborting the build by using the `abort_reason` parameter. This parameter takes a string and it will show up on your app’s build page.

```bash
curl -X POST -H "Authorization: ACCESS-TOKEN" "https://api.bitrise.io/v0.1/apps/APP-SLUG/builds/BUILD-SLUG/abort" -d '{"abort_reason": "aborted for a reason"}'
```

Normally, aborted builds count as failed builds. Use the `abort_with_success` parameter to abort a build but still count it as a successful one. The status report sent to your git provider will show the build as successful though on [bitrise.io](https://www.bitrise.io) it will be displayed as `Cancelled`.

```bash
curl -X POST -H "Authorization: ACCESS-TOKEN" "https://api.bitrise.io/v0.1/apps/APP-SLUG/builds/BUILD-SLUG/abort" -d '{"abort_with_success": true}'
```

Depending on your app settings, Bitrise might send email notifications to team members when a build is aborted. If you do not want notifications, set the `skip_notifications` parameter to `true`.

```bash
curl -X POST -H "Authorization: ACCESS-TOKEN" "https://api.bitrise.io/v0.1/apps/APP-SLUG/builds/BUILD-SLUG/abort" -d '{"skip_notifications": true}'
```
