Skip to content

Snippets: other systems

These are the snippets the console’s Snippets dialog offers under Other systems. Each is TypeScript; in a .js script the console shows the same code with its types removed. Every one runs unchanged in Code Studio’s own tests. How the API they call behaves is in writing scripts and the script API reference.

Call another system through a connection

Sends a request through a connection an administrator set up. The connection adds its credentials, so the script never holds them.

export default async function (event: { issue?: { key: string } } | null) {
const key = event?.issue?.key ?? 'DEMO-1';
const answer = await http.post<{ id: string }>('crm', '/api/tickets', { reference: key });
log.info(`CRM ticket ${answer.id} for ${key}`);
return { ticket: answer.id };
}

Read from an approved host

Reads a public API with fetch. The host must be approved in Code Studio settings, Hosts; no credentials are added.

export default async function () {
const response = await fetch('https://api.example.com/rates?base=EUR');
if (!response.ok) throw new Error(`The rates service answered ${response.status}`);
const rates: { rates: Record<string, number> } = await response.json();
return { usd: rates.rates.USD };
}

Handle a failed request

The verbs throw an HttpError on a status of 400 or more, carrying the status and Jira’s own message. Catch it to carry on past one missing work item.

export default async function () {
const missing: string[] = [];
for (const key of ['DEMO-1', 'DEMO-2']) {
try {
await jira.get(`/rest/api/3/issue/${key}`, { query: { fields: 'summary' } });
} catch (error) {
if (error instanceof HttpError && error.status === 404) missing.push(key);
else throw error;
}
}
return { missing };
}

A problem or a question? Write to support@oktul.com or open a request in the Help Center (opens in a new tab). Both reach the same service desk, so either way the request gets a reference and an SLA measuring the response.