# Fetch plugin

Fetch plugin — Legend State documentation.

Source: https://legend.so/state/v3/sync/fetch/

Legend State v3 documentation. Current documentation version.

`syncedFetch` is a simple wrapper around `fetch` to reduce boilerplate.

-   `get`: The URL to get. If it is an observable or Selector function, it will re-run whenever the value changes.
-   `set`: The URL to set
-   `getInit`: The `init` parameter to pass to `fetch` when getting
-   `setInit`: The `init` parameter to pass to `fetch` when setting. Defaults to `{ method: 'POST' }`
-   `valueType`: The function to call on the Response from get. Defaults to `json`.
-   `onSavedValueType`: The function to call on the Response from set. Defaults to the `valueType` option or `json`.
-   `onSaved`: Receives `{ saved, input, currentValue, props }`. Return a value to save back into the observable; `saved` is the parsed, transformed response from set.

Example:

```ts
import { syncedFetch } from '@legendapp/state/sync-plugins/fetch';
import { observable } from '@legendapp/state';

const state$ = observable(syncedFetch({
    get: 'https://url.to.get',
    set: 'https://url.to.set',
    onSaved: ({ saved }) => {
        return {
            updatedAt: saved.updatedAt
        }
    }
}))
```

[View Markdown](https://legend.so/state/v3/sync/fetch.md)[Report a documentation correction](https://github.com/LegendApp/legend-docs/issues/new?title=Docs%3A+Fetch+plugin&body=Page%3A+https%3A%2F%2Flegend.so%2Fstate%2Fv3%2Fsync%2Ffetch%0A%0AWhat+is+incorrect+or+missing%3F%0A%0AExpected+behavior+or+supporting+source%3A%0A)
