Stock media
Connect stock media providers with your own API keys.
The editor can search Pexels and Unsplash for photos, and GIPHY for stickers. You connect each service with your own account. Each one is off until you configure it.
This example connects all three through your own server, so that your keys stay on the server:
import { ImageEditor, builtInStickers, giphyStickers } from '@layermetry/media-editor/react';
export default function App() {
return (
<div style={{ position: 'fixed', inset: 0 }}>
<ImageEditor
licenseKey="YOUR_LICENSE_KEY"
onClose={() => console.log('The editor was closed')}
// Photos: your server adds your Pexels and Unsplash keys.
stockMediaApis={{
pexels: { endpointUrl: 'https://api.example.com/pexels' },
unsplash: { endpointUrl: 'https://api.example.com/unsplash', showAttribution: true },
}}
// Stickers: the built-in set, then GIPHY through your server.
stickers={{
sources: [builtInStickers(), giphyStickers({ endpointUrl: 'https://api.example.com/giphy' })],
}}
/>
</div>
);
}Each service takes exactly one of three connections:
| Connection | What happens | Use it for |
|---|---|---|
apiKey | The browser calls the service with your key. Anyone who opens the page can read the key. | Local tests |
endpointUrl | The browser calls your server, and your server calls the service with your key. | Production |
searchFn (Pexels, Unsplash) or requestFn (GIPHY) | The editor calls your function, and your function returns the answer. | Your own client code |
A searchFn receives the search (query, page, perPage, and more) and answers with { results, total, total_pages }. Each result is a StockPhoto items, both exported types. A StockPhoto has id and url, and can have thumbnailUrl, width, height, alt, attribution, and downloadTrackingUrl. The answer of Pexels or Unsplash, unchanged, is also accepted. The editor checks every page of results: an item without an id or a url is left out, with one console line, and an answer with no list of results is shown to the user as an error of that service.
A setting that names none, or more than one, of apiKey, endpointUrl, and searchFn turns off only that service. Its panel says, for example, "Unsplash photos are not available.", and the console says which setting is wrong.
Pexels
stockMediaApis.pexels connects Pexels. Default: none, and the Pexels tab does not appear.
stockMediaApis={{ pexels: { endpointUrl: 'https://api.example.com/pexels' } }}| Option | Default | Description |
|---|---|---|
showAttribution | true | false hides the photographer's name on the Pexels photo tiles. |
defaultQueries | ['popular'] | Search words shown as buttons under the search field. |
headers | none | Extra headers for requests to endpointUrl, for example your own session token. |
Unsplash
stockMediaApis.unsplash connects Unsplash. Unsplash requires that the photographer's name is shown, so showAttribution: true is required. Default: none, and the Unsplash tab does not appear.
stockMediaApis={{ unsplash: { endpointUrl: 'https://api.example.com/unsplash', showAttribution: true } }}| Option | Default | Description |
|---|---|---|
showAttribution | required, true | Shows the photographer's name on each photo. |
trackDownloads | true | Tells Unsplash each time a photo is used, as the Unsplash guidelines ask. |
defaultQueries | ['nature'] | Search words shown as buttons under the search field. |
headers | none | Extra headers for requests to endpointUrl. |
Photo credits
A stock photo keeps its photographer's credit after the user places it. The placed item carries attribution, which is saved with templates and drafts.
When the design holds a stock photo with a credit, the Export panel shows two switches:
| Switch | What it does |
|---|---|
| Show photo credits on the canvas | Draws the credits, such as "Photo by Ana on Unsplash", in a small dark box in the bottom-left corner of the canvas. |
| Include photo credits in the export | Writes the same box into the exported file. |
The user's choice is saved with the design, and it can be undone. showAttributionOverlay and includeAttributionInExport set the starting value of each switch. Both are false by default.
To show credits your own way, hide a switch. A hidden switch always follows your prop:
ui={{ exportOptions: { hidden: ['photoCreditsOverlay', 'photoCreditsInExport'] } }}stockMediaApis.pexels.showAttribution: false hides the photographer's name on the Pexels photo tiles. Unsplash tiles always show the name, because Unsplash requires it.
GIPHY
GIPHY is a source of stickers. You connect it in the stickers prop with giphyStickers, not in stockMediaApis. Default: the built-in stickers only.
import { builtInStickers, giphyStickers } from '@layermetry/media-editor/react';
// …
stickers={{ sources: [builtInStickers(), giphyStickers({ endpointUrl: 'https://api.example.com/giphy' })] }}| Option | Default | Description |
|---|---|---|
apiKey, endpointUrl, or requestFn | none, one is required | The connection. endpointUrl can be a path on your own site, such as /api/giphy. |
id | 'giphy' | The id of the source. |
label | 'GIPHY' | The name of the source in the Stickers panel. |
See Stickers and GIFs for what the user sees.
Where providers appear
| Service | Where the user finds it |
|---|---|
| Pexels, Unsplash | The Upload Images panel, on the Stock Images tab, with one tab for each service. |
| Unsplash | The Background panel, on the Image tab. The chosen photo fills the page. |
| GIPHY | The Stickers panel, as a source next to the built-in stickers. |
View full sizeThe screenshot shows pictures returned by the example host's searchFn. It is not a live search of a stock provider.
A search shows 15 photos at a time, and Load More shows the next 15. The editor asks for photos in the shape of the page: landscape, portrait, or square.