packages/docs/plugins/http.md
Desktop apps can make HTTP requests without CORS restrictions, but plugin code runs in a browser context. The HTTP API bridges this gap by routing plugin requests through Tauri's native HTTP client.
Use api.Http.fetch instead of the browser's fetch whenever you need to call external APIs from a plugin.
{% hint style="info" %}
Access HTTP via api.Http.fetch(url, init) in your plugin's lifecycle hooks. The signature matches the standard Fetch API.
{% endhint %}
{% tabs %} {% tab title="GET request" %}
import type { NuclearPluginAPI } from '@nuclearplayer/plugin-sdk';
export default {
async onEnable(api: NuclearPluginAPI) {
const response = await api.Http.fetch(
'https://api.example.com/tracks?q=radiohead'
);
if (response.ok) {
const data = await response.json();
api.Logger.info(`Found ${data.results.length} tracks`);
}
},
};
{% endtab %}
{% tab title="POST request" %}
import type { NuclearPluginAPI } from '@nuclearplayer/plugin-sdk';
export default {
async onEnable(api: NuclearPluginAPI) {
const response = await api.Http.fetch('https://api.example.com/scrobble', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
artist: 'Radiohead',
track: 'Paranoid Android',
}),
});
if (!response.ok) {
api.Logger.error(`Scrobble failed: ${response.status}`);
}
},
};
{% endtab %} {% endtabs %}
When you call api.Http.fetch(url, init):
RequestInitRecord<string, string> and the body is read as a stringResponse object and returns itThis means you can use the response exactly like a normal fetch response: call .json(), .text(), check .ok and .status, read .headers, and so on.
FormData, Blob, ArrayBuffer, or other non-string types, the body is silently dropped. Serialize to a string (e.g., JSON.stringify) before sending.Record<string, string>. Multi-value headers (e.g., multiple Set-Cookie values) are not preserved.api.Http.fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>
The signature matches the standard fetch function. Refer to MDN's Fetch API docs for RequestInit options.