Snippets: other systems
Oktul Code Studio script snippets for requests to other systems through connections and approved hosts, copied from the console and run by its tests.
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, with no credentials. The host must be approved under Hosts in Code Studio settings.
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 status 400 or more, with the status and Jira’s message. Catch it to skip a missing work item and carry on.
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 };}Related
A problem or a question? Write to support@oktul.com or open a request in the Help Center (opens in a new tab).