ImageConfiguration

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:

src/App.tsx
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:

ConnectionWhat happensUse it for
apiKeyThe browser calls the service with your key. Anyone who opens the page can read the key.Local tests
endpointUrlThe 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

Requires a Pexels API key, from your Pexels account.

stockMediaApis.pexels connects Pexels. Default: none, and the Pexels tab does not appear.

stockMediaApis={{ pexels: { endpointUrl: 'https://api.example.com/pexels' } }}
OptionDefaultDescription
showAttributiontruefalse hides the photographer's name on the Pexels photo tiles.
defaultQueries['popular']Search words shown as buttons under the search field.
headersnoneExtra headers for requests to endpointUrl, for example your own session token.

Unsplash

Requires an Unsplash access key, from your Unsplash developer account.

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 } }}
OptionDefaultDescription
showAttributionrequired, trueShows the photographer's name on each photo.
trackDownloadstrueTells Unsplash each time a photo is used, as the Unsplash guidelines ask.
defaultQueries['nature']Search words shown as buttons under the search field.
headersnoneExtra 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:

SwitchWhat it does
Show photo credits on the canvasDraws 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 exportWrites 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

Requires a GIPHY API key, from your GIPHY developer account.

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' })] }}
OptionDefaultDescription
apiKey, endpointUrl, or requestFnnone, one is requiredThe 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

ServiceWhere the user finds it
Pexels, UnsplashThe Upload Images panel, on the Stock Images tab, with one tab for each service.
UnsplashThe Background panel, on the Image tab. The chosen photo fills the page.
GIPHYThe Stickers panel, as a source next to the built-in stickers.
The Stock tab with configured photo-service tabs, search and original campaign photographsView full size

The 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.

On this page