Files
supabase/apps/reference/_supabase_js_versioned_docs/version-v1/invoke.mdx
2022-08-03 21:03:58 +02:00

179 lines
3.4 KiB
Plaintext

---
id: invoke
title: "invoke()"
slug: invoke
custom_edit_url: https://github.com/supabase/supabase/edit/master/web/spec/supabase.yml
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
Invokes a Supabase Function.
:::caution
Edge Functions are Experimental until 1 August 2022. There will breaking changes. Do not use them in Production.
:::
We have released Supabase Functions to gather feedback. You're welcome to try them out and send us feedback but we strongly advise against using them for anything in production.
There will be breaking changes with no notice.
<Tabs
defaultValue="js"
groupId="reference/supabase-js"
values={[{ label: 'JavaScript', value: 'js' }]}>
<TabItem value="js">
```js
const { data: user, error } = await supabase.functions.invoke('hello', {
body: JSON.stringify({ foo: 'bar' })
})
```
</TabItem>
</Tabs>
## Parameters
<ul className="method-list-group">
<li className="method-list-item">
<h4 className="method-list-item-label">
<span className="method-list-item-label-name">
functionName
</span>
<span className="method-list-item-label-badge required">
required
</span>
<span className="method-list-item-validation">
<code>string</code>
</span>
</h4>
<div class="method-list-item-description">
the name of the function to invoke
</div>
</li>
<li className="method-list-item">
<h4 className="method-list-item-label">
<span className="method-list-item-label-name">
invokeOptions
</span>
<span className="method-list-item-label-badge false">
optional
</span>
<span className="method-list-item-validation">
<code>FunctionInvokeOptions</code>
</span>
</h4>
<div class="method-list-item-description">
object with the following properties
`headers`: object representing the headers to send with the request
`body`: the body of the request
`responseType`: how the response should be parsed. The default is `json`
</div>
</li>
</ul>
## Notes
- Requires an Authorization header.
- Invoke params generally match the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) spec.
## Examples
### Basic invocation.
<Tabs
defaultValue="js"
groupId="reference/supabase-js"
values={[{ label: 'JavaScript', value: 'js' }]}>
<TabItem value="js">
```js
const { data: user, error } = await supabase.functions.invoke('hello', {
body: JSON.stringify({ foo: 'bar' })
})
```
</TabItem>
</Tabs>
### Specifying response type.
By default, `invoke()` will parse the response as JSON. You can parse the response in the following formats: `json`, `blob`, `text`, and `arrayBuffer`.
<Tabs
defaultValue="js"
groupId="reference/supabase-js"
values={[{ label: 'JavaScript', value: 'js' }]}>
<TabItem value="js">
```js
const { data: user, error } = await supabase.functions.invoke('hello', {
responseType: 'text',
body: JSON.stringify({ foo: 'bar' })
})
```
</TabItem>
</Tabs>
### Parsing custom headers.
You can pass custom headers to your function. Note: supabase-js automatically passes the `Authorization` header with the signed in user's JWT.
<Tabs
defaultValue="js"
groupId="reference/supabase-js"
values={[{ label: 'JavaScript', value: 'js' }]}>
<TabItem value="js">
```js
const { data: user, error } = await supabase.functions.invoke('hello', {
headers: {
"my-custom-header": 'my-custom-header-value'
},
body: JSON.stringify({ foo: 'bar' })
})
```
</TabItem>
</Tabs>