Picsha.ai React SDK (@picsha-ai/react)
Overview
The strictly-typed React SDK provides ready-to-use components for securely uploading files to the Picsha AI backend and rendering assets with dynamic AI transformations.
Installation
npm install @picsha-ai/react
Components
1. <PicshaImage />
A drop-in replacement for the standard <img> tag that automatically constructs delivery URLs and applies on-the-fly transformations via the Picsha backend.
Key Features:
- Typed Transformations: Pass transformations as native React props (
blur={5},radius="max"). - AI-Powered Effects: Supports
removeBackground,generativeRemove, and automaticaspectRatiocropping. - Overlays: Supports complex arrays of text and image overlays, automatically encoding them into the delivery URL.
Usage:
import { PicshaImage } from '@picsha-ai/react';
<PicshaImage
deliveryEndpoint="https://cdn.picsha.ai"
assetId="550e8400-e29b-41d4-a716-446655440000"
width={800}
aspectRatio="16:9"
position="attention"
radius={16}
overlays={[
{ type: 'text', text: 'Stunning Event', gravity: 'bottom', size: 48 }
]}
/>
Props:
deliveryEndpoint(string): Base API URLassetId(string) orurl(string): The asset to render or external URL to proxy mapping to/v1/fetchwidth,height,aspectRatio(number | string): Dimensionsblur,radius: Styling adjustmentsformat(including"auto"),quality,fit,crop,position,background: Standard image optimizationsremoveBackground(boolean),generativeRemove(string): AI capabilitiesmimi(string): Natural-language generative edit prompt (MIMI)mimiMode('lite' | 'standard' | '4k'): MIMI quality tier (defaultstandard)upscale('2k' | '4k'): Free preview-faithful export upscale, one tier upmimiBg(string): Generative background swap prompt (subject preserved pixel-exact)overlays(OverlayConfig[]): Array of text/image layers to compositeforce(boolean): Bypasses CDN cachecb(string): Cache-busting parametersig(string): Delivery signature for generative props — see belowsignedUrl(string): A complete pre-signed delivery URL, rendered as-is (all other transformation props are ignored)
Authorization for generative props (v2.1+)
Billed generative parameters (mimi, mimiBg, generativeRemove, removeBackground) are rejected with 401 when requested anonymously — a browser <img> cannot carry your API key, and it never should. Plain transformation props (dimensions, crop, format, quality, overlays) work anonymously as before. Two ways to render generative images on public pages:
Option A — signedUrl (simplest): call POST /assets/{id}/sign-delivery server-side and hand the resulting URL to the component untouched:
<PicshaImage signedUrl={signedUrlFromYourApi} alt="Generated scene" />
Option B — sig + matching props: the signature covers the exact query permutation, so build it server-side from the same props using the exported buildPicshaQueryParams helper:
// server
import { buildPicshaQueryParams } from '@picsha-ai/react';
const queryParams = buildPicshaQueryParams({ width: 800, mimi: 'golden hour' });
const { signature } = await picshaPost(`/assets/${id}/sign-delivery`, {
urlPath: `/v1/assets/${id}/render`, queryParams,
});
// client — props MUST match what was signed, or the CDN refuses the request
<PicshaImage deliveryEndpoint="..." assetId={id} width={800} mimi="golden hour" sig={signature} />
The
urlprop proxies through/v1/fetch, which now requires authentication (it was hardened against server-side request forgery). It can no longer be used from the browser — ingest external URLs into Picsha server-side instead. The component logs a console warning when it is used.
2. <PicshaUploadWidget />
A robust, themeable upload widget built on top of Uppy. It automatically handles secure authentication, direct-to-S3 uploads, and post-upload registration with the Picsha AI backend.
Key Features:
- Secure Integration: Automatically requests signed URLs via
/v1/assets/presignedbefore uploading to S3. - Resumable Uploads: Pass
useResumable={true}to activate theTusprotocol for robust chunked uploads for large assets. - Auto-Registration: After a successful upload, seamlessly calls
POST /v1/assetsto finalize the asset directly from the client.
Usage:
import { PicshaUploadWidget } from '@picsha-ai/react';
<PicshaUploadWidget
apiUrl="https://api.picsha.ai"
getToken={async () => (await fetch('/api/picsha-token')).text()}
useResumable={true}
maxFileSize={1024 * 1024 * 1024} // 1GB (default is 2GB)
theme="dark"
onUploadSuccess={(asset) => console.log('Asset ready:', asset)}
/>
Tracking upload state:
const [uploading, setUploading] = useState(false);
<PicshaUploadWidget
apiUrl="https://api.picsha.ai"
getToken={async () => (await fetch('/api/picsha-token')).text()}
onIsUploadingChange={setUploading}
onUploadSuccess={(asset) => console.log('Asset ready:', asset)}
/>
false means the files have finished uploading, whether or not every file succeeded. It can arrive more than once for the same batch, so treat the value as state rather than counting calls. onUploadSuccess fires separately for each file once it is registered with Picsha, which can be a moment after false.
Props:
apiUrl(string): Base API URLgetToken(async function): Returns the credential sent asAuthorization: Bearer <token>on every upload request. Use a Picsha session token for the signed-in user, or a short-lived token your own backend mints. Never yoursk_live_...secret key — it grants full delete rights over the organization and there is no browser-safe publishable key (see Architecture & Concepts).useResumable(boolean): Activates the TUS protocol for large files instead of standard presigned putsonUploadSuccess(function): Callback with full asset details once registeredonError(function): Error handling callbackonIsUploadingChange(function): Called withtruewhen a batch of uploads starts andfalsewhen it finishes or is cancelled. Use it to keep a dialog open or block navigation while files are in flight. Requires@picsha-ai/react2.3.0 or latermaxFileSize(number): Limit maximum bytes allowed (default 2GB)autoProceed(boolean): Start uploading as soon as files are added, without waiting for the Upload button (defaultfalse)config(object): Sent with each file when it is registered.tags(string[]) andmetadata(object) are attached to the asset; every other key is passed as the ingestconfig(auto_tag,remove_background,expires_at, …) — see Ingesttheme('light' | 'dark' | 'auto'): Widget themewidth,height(number | string): Size of the widget (defaults'100%'and450)companionUrl(string): URL of an Uppy Companion server you run. When set, the widget offers cloud import tabs alongside local files. Omit it for local uploads only. Requires@picsha-ai/react2.3.0 or latercompanionSources(string[]): Which cloud sources to offer whencompanionUrlis set (default['Dropbox', 'GoogleDrive', 'Url']). Each must also be configured on your Companion serverorgId(string): Optional Organization tag