The Adaptavist Group LogoDocumentation

HAPI

Learn how HAPI simplifies scripting with type-checked operations and readable field aliases, which modules to import, and how to structure a HAPI script.

HAPI is an API (application programming interface) that simplifies common tasks such as:

  • Creating and managing work items.
  • Searching and updating work items.
  • Working with attachments, comments, links, and spaces.
  • Transitioning work items through workflows.

You can use HAPI directly in your ScriptRunner scripts as a simpler alternative to Jira's regular API.

Tip: Terminology

Atlassian has introduced updated terminology for issues (now referred to as work items) and projects (now referred to as spaces). In this documentation, a work item corresponds to a Jira issue (Task, Bug, Story, Sub-task, etc.), and a space is equivalent to a Jira project.

In code and in the Jira APIs, you will still see the original terms issue and project, for example, in Issues.create, Projects.getByKey, and projectTypeKey. When you read code, treat an issue as a work item and a project as a space.

Why use HAPI?

HAPI is more than just a convenience wrapper around the Jira REST API. Two key benefits are:

  • Type-checked fields and operations: HAPI surfaces Jira objects as TypeScript-friendly APIs. In the script editor, you get autocomplete for functions and fields (for example, summary, description, SelectListA), and type-checking on what you pass and what you read (for example, objects vs strings, arrays vs single values).
  • Friendly field aliases instead of raw field IDs: You don't have to work with customfield_12345. HAPI lets you use human-readable field aliases (for example, SelectListA, MultiSelectA, FreeTextA) when setting fields (such as in Issues.create and Issues.update) and when reading them (such as from issue.fields or issue.renderedFields).

Whenever you see custom field names used directly in code, those are HAPI aliases behind the scenes that resolve to your actual Jira custom fields.

Getting started with HAPI

Where HAPI is available

HAPI is built into every ScriptRunner script editor, so you can start using it immediately.

In each script editor you'll also find Example scripts. Most of these examples use HAPI and can be copied, pasted, and adapted directly in the editor to match your requirements.

Modules available in the script editor

Tip: What are modules?

A module is a package of related functionality that you can reuse in your scripts. Instead of writing everything from scratch, you import a module to gain access to its functions and types. For example, the @scriptrunnerhq/hapi/issues module gives you functions for working with work items (issues), while @scriptrunnerhq/hapi/projects focuses on spaces (projects). Importing these modules makes those operations available in your script editor.

The following modules are available in every script editor:

ModuleOperations on
@scriptrunnerhq/hapi/issuesWork items (issues)
@scriptrunnerhq/hapi/projectsSpaces (projects)
@scriptrunnerhq/hapi/usersUser accounts
@scriptrunnerhq/hapi/groupsGroups and their membership
@scriptrunnerhq/hapi/rolesSpace roles
@scriptrunnerhq/hapi/versionsVersions in a space
@scriptrunnerhq/hapi/fieldsCustom field definitions
@scriptrunnerhq/hapi/resolutionsResolutions
@scriptrunnerhq/hapi/adminBulk administrative operations
@scriptrunnerhq/hapi/paginated-iteratorWorking through paged results

You can import them as follows:

import * as Issues from '@scriptrunnerhq/hapi/issues'
import * as Projects from '@scriptrunnerhq/hapi/projects'
import * as Users from '@scriptrunnerhq/hapi/users'
Tip: Imports are added for you

You do not have to write the import yourself. Start typing a function name, such as getByKey, and select it from the suggestions. The editor adds the matching import to the top of your script, unless it is already there.

Note: Field behaviours are the exception. They run in the user's browser rather than on the server, so only issues, fields, projects and paginated-iterator are available in them.

How HAPI scripts are structured

Every HAPI script follows the same basic pattern:

  1. Import the HAPI modules you need. At the top of your script, you pull in the functionality you want to use. For example, @scriptrunnerhq/hapi/issues gives you functions for working with work items (issues). Without importing a module, its functions won't be available in your script.
  2. Export a default async function. ScriptRunner runs whatever you export as the default function from your script file. Marking this function as async tells TypeScript that it will perform time-consuming work (such as calling Jira over the network) and return a Promise when it finishes. ScriptRunner waits for this function to complete before it considers the script done.
  3. Call HAPI functions inside that function. All your actual logic, such as creating work items, updating them, searching, etcs, goes inside the default function. Because HAPI functions (like Issues.create and Issues.update) are asynchronous, you usually call them with await so you can use their results directly.
  4. Optionally return a value and/or write to the log. You can return data (for example, the work item you just created), which is useful when you want to see the result, pass it to another tool, or debug your script. You can also use console.log(...) to write messages to the log.

For example, the following script creates a basic Jira work item with only the required fields:

import * as Issues from '@scriptrunnerhq/hapi/issues'
 
export default async function () {
  const issue = await Issues.create({
    body: {
      fields: {
        project: {
          // change this to a valid project key
          key: 'JRA',
        },
        issuetype: {
          // ensure this issue type is valid for your project
          name: 'Task',
        },
        summary: 'Test issue',
      },
    },
  })
 
  return issue
}

The default export is async because HAPI operations such as Issues.create and Issues.update return Promises. You can return values (useful for debugging or pipelines) and use console.log for logging.

HAPI hints and tips

Use Ctrl+Space to redisplay code completions

When using HAPI in the script editor, the Control + Space keyboard shortcut is very useful. It shows code completions, and you can press it again if completions disappear.

You might want to redisplay completions when:

  • You started typing and chose the wrong value, then deleted some text and completions stopped showing.
  • You clicked out of the script console and came back, and completions no longer appear.
  • You removed an option inside a string and want to see all available options again.

Press Ctrl + Space at any point to bring the suggestions back.

Use in-app example scripts

The script editor includes example scripts that already use HAPI, so you can get started quickly without writing everything from scratch. These examples:

  • Show common tasks, such as creating or updating work items and working with spaces.
  • Use the same HAPI modules you'll use in your own scripts, so you can copy, paste, and adapt them.

To open them, click the Example script button in the script editor. From there, choose an example that's close to what you want to do, and then modify it to match your requirements.

Compatibility

HAPI is compatible with Jira Software and Jira Service Management (JSM).

Search documentation

Start typing to search the docs.