How to call a 3rd party API and update fields on a form using Sanity document actions or a custom input component.
6 replies
Last updated: Mar 28, 2023
C
anyone have a code example or if it's possible, on a page, you click a button and call a 3rd party API and update fields on that form and then disable them, using for reference only.
Z
I'd take a look at actions: https://www.sanity.io/docs/document-actions
It’s possible with document actions or a custom input component. I haven’t migrated this to V3, but here’s a V2 component you can use to get started.
{
name: 'fetchObject',
description:
'A component that will fetch data from the specified URL and write something to the field below',
title: 'Fetch Stuff with This Object',
type: 'object',
fields: [
{
name: 'url',
title: 'Url to Fetch',
type: 'url',
inputComponent: UrlWithButton,
},
{
name: 'fieldToWrite',
title: 'Field to Write',
type: 'string',
},
],
},import React from 'react';
import { Card, TextInput, Button } from '@sanity/ui';
import { FormField } from '@sanity/base/components';
import PatchEvent, { set, unset } from '@sanity/form-builder/PatchEvent';
import { useId } from '@reach/auto-id';
import { withDocument } from 'part:@sanity/form-builder';
import { studioClient } from '../../lib/utils/studioClient';
const UrlWithButton = React.forwardRef((props, ref) => {
const {
type, // Schema information
value, // Current field value
readOnly, // Boolean if field is not editable
markers, // Markers including validation rules
presence, // Presence information for collaborative avatars
compareValue, // Value to check for "edited" functionality
onFocus, // Method to handle focus state
onBlur, // Method to handle blur state
onChange, // Method to handle patch events,
document,
} = props;
// Creates a change handler for patching data
const handleChange = React.useCallback(
// useCallback will help with performance
event => {
const inputValue = event.currentTarget.value; // get current value
// if the value exists, set the data, if not, unset the data
onChange(PatchEvent.from(inputValue ? set(inputValue) : unset()));
},
[onChange]
);
//Fetches the URL specfied then uses the JS client to write to the current document
const handleClick = async () => {
const res = await fetch(value).then(res => res.json());
studioClient
.patch(document._id)
.set({
fetchObject: {
...document.fetchObject,
fieldToWrite: res.data[3].breed,
},
})
.commit();
};
const inputId = useId();
return (
<FormField
description={type.description} // Creates description from schema
title={type.title} // Creates label from schema title
__unstable_markers={markers} // Handles all markers including validation
__unstable_presence={presence} // Handles presence avatars
compareValue={compareValue} // Handles "edited" status
inputId={inputId} // Allows the label to connect to the input field
>
<Card padding={2}>
<TextInput
id={inputId} // A unique ID for this input
fontSize={2}
padding={3}
space={[3, 3, 4]}
value={value} // Current field value
readOnly={readOnly} // If "readOnly" is defined make this field read only
onFocus={onFocus} // Handles focus events
onBlur={onBlur} // Handles blur events
ref={ref}
onChange={handleChange} // A function to call when the input value changes
/>
</Card>
<Card padding={2}>
<Button
fontSize={2}
padding={3}
text='Fetch'
tone='primary'
onClick={handleClick}
/>
</Card>
</FormField>
);
});
export default withDocument(UrlWithButton);J
Hey
My custom field looks like this. I'm trying to update the field
user M
would love a bit of help with this! ❤️My custom field looks like this. I'm trying to update the field
collectionID, after pulling data from a third party when triggering a custom button.
J
My custom input field like thise
J
How can I update the object field when I trigger the button?
J
(FYI: This is exactly what I'm trying to achieve, expect it's not an input that triggers the action but a button: https://youtu.be/RJSG0BRftoc?t=145 )
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.