Lens Documentation

Web SDK Reference

Setup

Script Import

The Web SDK is bundled as a JavaScript module that includes everything, from the camera to the CSS, in a single import. It’s hosted by Truepic on our CDN for speed and convenience, and is evergreen with updates rolling out automatically, without any code redeployment on your end.

<script type="module">
  import useLensCamera from 'https://lens-sdk.truepic.com/lens_camera.es.js'
</script>

useLensCamera()

useLensCamera(config)

The main entrypoint that initializes the SDK and appends a hidden <lens-camera> web component to the <body> of the page.

Parameters

NameTypeDescriptionDefault
configobjectConfiguration options to initialize the SDK and <lens-camera> web component with.
apiKeystringThe API key issued by the Truepic team for your Web SDK integration.
appVersionstringThe version of your web app from your package.json or elsewhere.

Returns

The <lens-camera> web component instance. If called more than once, the same instance is returned, as only one can exist on the page.

Camera Component

The <lens-camera> web component displays the camera and provides a client-side JavaScript API to programmatically orchestrate it. Creation is handled by useLensCamera and should not be done manually.

Constants

NameTypeDescription
EVENTSobjectThe various event types.
CAPTUREstringThe capture event type.
CLOSEDstringThe closed event type.
ERRORstringThe error event type.
OPENEDstringThe opened event type.
UPLOADEDstringThe uploaded event type.
UPLOADINGstringThe uploading event type.
FACINGobjectThe front or back camera to use based on the direction it’s facing.
ENVIRONMENTstringThe back camera facing the environment.
USERstringThe front camera facing the user.
MODESobjectThe capture mode to use, either photo or video.
PHOTOstringThe capture mode to take a photo.
VIDEOstringThe capture mode to record a video.
VERSIONstringThe version of the SDK that’s currently in use.

Methods

close()

close()

Close the camera and stop accessing the user’s location, camera, and microphone (video mode only).

Parameters

None

Returns

undefined

getUploadCounts()

getUploadCounts()

Get the number of captures in the various states of being uploaded.

Parameters

None

Returns

object

NameTypeDescription
waitingnumberThe number of captures waiting to be uploaded.
activenumberThe number of captures in the process of being uploaded.
totalnumberThe total number of uploads both waiting and active.

hasUploadPending()

hasUploadPending()

Whether there are any captures being uploaded.

Parameters

None

Returns

boolean

open()

open((options = {}))

Open the camera and start the initialization process to begin taking captures. The browser will prompt the user for access to their location, camera, and microphone (video mode only) if they haven’t previously granted access (each browser does this differently).

Parameters

NameTypeDescriptionDefault
optionsobjectConfiguration options to open the camera with.{}
customDataobjectCustom data to associate with each capture, such as an identifier to connect the captures to after processing. Any JSON-encodable values are allowed.null
facingFACING constantThe front or back camera to use based on the direction it’s facing.FACING.ENVIRONMENT
modeMODES constantThe capture mode to open the camera in, either photo or video.MODES.PHOTO
processOptionsobjectOptions to configure how each capture should be processed by the API. See the API reference for a complete list of options.null

Returns

undefined

remove()

remove()

Close (if open) and remove the <lens-camera> web component from the page.

Parameters

None

Returns

undefined

Events

An event is an instance of CustomEvent with a detail property that contains the custom data documented for each type below. For convenience, the EVENTS constants can be used to reference a type when adding or removing a listener.

capture

Fired when a photo or video has been captured successfully.

Detail

NameTypeDescription
captureCapture ObjectThe data model for the capture.

closed

Fired when close has completed successfully.

Detail

None

error

Fired when an error occurs that may be useful to your integration. See Error Objects for more details.

Detail

NameTypeDescription
captureCapture ObjectThe data model for the capture.
errorLensError ObjectThe error that occurred.

opened

Fired when open has completed successfully.

Detail

None

uploaded

Fired when a capture has been successfully uploaded to the Lens API for verification and processing.

Detail

NameTypeDescription
captureCapture ObjectThe data model for the capture.

uploading

Fired when a capture starts uploading to the Lens API for verification and processing.

Detail

NameTypeDescription
captureCapture ObjectThe data model for the capture.

Capture Object

The data model for a photo or video capture.

Properties

NameTypeDescription
blobBlobThe raw, binary JPEG photo or MP4 video.
capturedAtstring (date-time)The date/time of capture.
customDataobjectCustom data associated with the capture, such as an identifier to connect the capture to after processing.
durationintegerIf video, the duration in seconds.
posterBlobBlobIf video, the first frame as a raw, binary JPEG for use as the poster or preview.
processOptionsobjectOptions to configure how the capture should be processed by the API.
recordobjectThe latest capture record from the Lens API. Only set after uploading.
typeTYPES constantThe type of capture.

Constants

NameTypeDescription
TYPESobjectThe type of capture.
PHOTOstringA photo taken.
VIDEOstringA video recorded.

Methods

hashBlob()

async hashBlob()

The SHA-256, base-64 encoded hash of the blob returned by a capture.

Parameters

None

Returns

A promise that resolves with the SHA-256, base-64 encoded hash, or undefined if there is no blob. The promise is rejected if an error occurs.

isPhoto()

isPhoto()

Whether the capture is a photo.

Parameters

None

Returns

boolean

isVideo()

isVideo()

Whether the capture is a video.

Parameters

None

Returns

boolean

Error Objects

The <lens-camera> web component gracefully handles and displays all errors produced by the Web SDK. However, when an error may be useful to your integration as well, it’s propagated through the error event. Listening to this event is completely optional and only necessary for more in-depth integrations.

To check for a specific type of error, the error classes can be imported by name and compared against using instanceof:

import { LensUploadError } from 'https://lens-sdk.truepic.com/lens_camera.es.js'

// ...

if (error instanceof LensUploadError) {
  // Handle this type of error specifically...
}

LensError

The base class that all Web SDK errors extend and inherit from. Extends and inherits from the global Error object itself.

Properties

NameTypeDescription
codeintegerThe CODES constant that specifically identifies the error.
originalErrorErrorThe underlying, original error that prompted this error.

See additional inherited properties from Error

Constants

NameTypeDescription
CODESobjectIdentifiers for each specific error.

See additional inherited constants from Error

Methods

None. See inherited methods from Error.

LensUploadError

The error when a capture fails to be uploaded and cannot be retried. Extends and inherits from LensError.

Properties

None. See inherited properties from LensError.

Constants

NameTypeDescription
CODESobjectIdentifiers for each specific error.
NOTIFY_REQUEST_FAILEDintegerThe initial request failed to notify the Lens API that a capture is pending upload.
UPLOAD_REQUEST_FAILEDintegerThe upload request failed to the Lens API.

See additional inherited constants from LensError

Methods

None. See inherited methods from LensError.

Code Copied

Glossary

Search Terms

  • API

  • C2PA

  • CAI

  • Controlled Capture

  • Entropy

  • EXIF

  • Geocoded

  • IDE

  • JPEG

  • JSON

  • Lens SDK

  • Provenance

  • Recapture

  • Reverse Image Search

  • XMP