# 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.
>
> Documentation for Oktul Code Studio, for Jira Cloud and Confluence Cloud, by Oktul: https://oktul.com/docs/plugins/code-studio/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](../../writing-scripts/) and the [script API reference](../../script-api/).

## 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.

```ts
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.

```ts
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.

```ts
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

- [Writing scripts](../../writing-scripts/)
- [Requests to other systems](../../requests-to-other-systems/)
- [Script examples](../../script-examples/)

---

A problem or a question? Write to [support@oktul.com](mailto:support@oktul.com) or open a request in the [Help Center](https://oktul.atlassian.net/servicedesk/customer/portals). Both reach the same service desk, so either way the request gets a reference and an SLA measuring the response.
