Skip to main content

Optical Character Recognition (OCR) from Cape.js

Cape's secure OCR service confidentially detects text within pdf documents and returns a transcript. This guide will walk you through the steps required to get started quickly using Cape.js.

Install the Cape CLI

The Cape CLI is used to manage your Cape account and resources. In this guide, it will be used to sign up for a Cape account and to create a personal access token for authentication. The following command will download the appropriate version for your OS and platform, and place it under $HOME/.cape/bin:

curl -fsSL https://raw.githubusercontent.com/capeprivacy/cli/main/install.sh | sh

Note: sudo may be required when running this command.

Sign up for Cape

Sign up for Cape by simply running cape signup (or cape login if you've logged in before). Cape uses your Github account for login.

cape signup

If your terminal is able to, it will auto-launch a browser (if not, open the link provided manually). Finish the sign-up process with your browser by confirming that the code you see there matches the code you see in your terminal.

Create a personal access token (PAT)

Your personal access token will be used within your JavaScript application, and is used to identify you when making a request to the Cape OCR service. Create your PAT using the following command:

cape token create --name my-token --description 'for use with js app calling Cape OCR'

Which produces:

Success! Your token: <token string>

_Note: The --name and --description can be anything you'd like to help you identify and manage the token later.

Configure your JavaScript application

Cape provides multiple options to install the Cape JavaScript SDK:

npm install @capeprivacy/cape-sdk

If you are using yarn:

yarn add @capeprivacy/cape-sdk

Or if you are using pnpm:

pnpm add @capeprivacy/cape-sdk

After choosing your preferred method, import the SDK into your project in one of the following ways:

ES module style: Use the import statement (recommended):

import { Cape } from "@capeprivacy/cape-sdk";

CommonJS style: Use the const statement:

const { Cape } = require("@capeprivacy/cape-sdk");

Script tag: Use the script tag in an HTML document:

<script type="module">
import { Cape } from "https://cdn.skypack.dev/@capeprivacy/cape-sdk";
</script>

Then use your token as the authToken parameter when you instantiate your Cape instance:

const authToken = "<your token>";
const client = new Cape({ authToken });

Invoke the OCR service

Finally, use the function ID BphLNvuBsVzBKk89hhgkkq to specify the OCR service when calling Cape.

When invoking cape.run:

const result = await client.run({ id: `BphLNvuBsVzBKk89hhgkkq`, data });

Or in cape.connect when using cape.invoke:

try {
await cape.connect({ id: `BphLNvuBsVzBKk89hhgkkq` });

const results = await Promise.all([
cape.invoke({ data: "YOUR_DATA_1" }),
cape.invoke({ data: "YOUR_DATA_2" }),
cape.invoke({ data: "YOUR_DATA_3" }),
]);
} catch (error) {
console.error('Something went wrong', error);
}

Next steps

Did you know that Cape also allows you to create and host your own secure functions? The Cape CLI can also deploy code that you write, and then you can invoke them with the assurance that both the code and the data will be protected. Learn more.

Join the community


Note

We are actively soliciting feedback from our beta. Please use this form to provide feedback.