Options
All
  • Public
  • Public/Protected
  • All
Menu

@uploadcare/rest-client

Uploadcare REST API Client

This is an Uploadcare REST API wrapper to work with Node.js and browser.

API Reference

Build Status NPM version GitHub release  Uploadcare stack on StackShare

Install

npm install @uploadcare/rest-client

Usage

Authentication

Every REST API request should be authenticated using your secret key.

According to the spec, there are two available authentication methods:

  1. Uploadcare.Simple
  2. Uploadcare

Uploadcare.Simple authentication method

With the Uploadcare.Simple authentication method, your secret key gets included in every request. This method isn't secure enough because secret key is exposed to the runtime and will be transmitted over the network.

⚠️We strongly recommend not to use this method in production, especially on the client-side.⚠️

Example:

import { listOfFiles, UploadcareSimpleAuthSchema } from '@uploadcare/rest-client';

const uploadcareSimpleAuthSchema = new UploadcareSimpleAuthSchema({
publicKey: 'YOUR_PUBLIC_KEY',
secretKey: 'YOUR_SECRET_KEY',
});

const result = await listOfFiles({}, { authSchema: uploadcareSimpleAuthSchema })

Uploadcare authentication method

With the Uploadcare authentication method, your secret key is used to derive signature but isn't included in every request itself.

Builtin signature resolver

You can use the builtin signature resolver, which automatically generates signature in-place using crypto module at Node.js or Web Crypto API at browsers.

import { UploadcareAuthSchema } from '@uploadcare/rest-client';

new UploadcareAuthSchema({
publicKey: 'YOUR_PUBLIC_KEY',
secretKey: 'YOUR_SECRET_KEY',
})

⚠️We strongly recommend not to use builtin signature resolver on the client-side.⚠️

Custom signature resolver

This option is useful on the client-side to avoid secret key leak. You need to implement some backend endpoint, which will generate signature. In this case, secret key will be stored on your server only and will not be disclosed.

import { UploadcareAuthSchema } from '@uploadcare/rest-client';

new UploadcareAuthSchema({
publicKey: 'YOUR_PUBLIC_KEY',
signatureResolver: async (signString) => {
/**
* You need to make HTTPS request to your backend endpoint,
* which should sign the `signString` using secret key.
*/
const signature = await yourBackendApi.getSignature(signString)
return signature
}
})

API

You can use low-level wrappers to call the API endpoints directly:

import { listOfFiles, UploadcareSimpleAuthSchema } from '@uploadcare/rest-client';

const uploadcareSimpleAuthSchema = new UploadcareSimpleAuthSchema({
publicKey: 'YOUR_PUBLIC_KEY',
secretKey: 'YOUR_SECRET_KEY',
});

const result = await listOfFiles({}, { authSchema: uploadcareSimpleAuthSchema })

List of all available API methods is available at the rest-client API Reference.

Settings

List of all available Settings is available at the rest-client API Reference.

We have the only two paginatable API methods - listOfFiles and listOfGroups. You can use one of those methods below to paginate over.

Using async generator and paginate() helper

import { listOfFiles, paginate } from '@uploadcare/rest-client'

const uploadcareSimpleAuthSchema = new UploadcareSimpleAuthSchema({
publicKey: 'YOUR_PUBLIC_KEY',
secretKey: 'YOUR_SECRET_KEY',
});

const paginatedListOfFiles = paginate(listOfFiles)
const pages = paginatedListOfFiles({}, { authSchema: uploadcareSimpleAuthSchema })

for await (const page of pages) {
console.log(page)
}

Using Paginator class

import { listOfFiles, Paginator } from '@uploadcare/rest-client'

const uploadcareSimpleAuthSchema = new UploadcareSimpleAuthSchema({
publicKey: 'YOUR_PUBLIC_KEY',
secretKey: 'YOUR_SECRET_KEY',
});

const paginator = new Paginator(listOfFiles, {}, { authSchema: uploadcareSimpleAuthSchema })

while(paginator.hasNextPage()) {
const page = await paginator.next()
console.log(page)
}

while(paginator.hasPrevPage()) {
const page = await paginator.prev()
console.log(page)
}

console.log(paginator.getCurrentPage())

Check out the rest-client API Reference for the Paginator.

Job status polling

There are no helpers for the job status polling yet. Stay tuned.

Security issues

If you think you ran into something in Uploadcare libraries that might have security implications, please hit us up at bugbounty@uploadcare.com or Hackerone.

We'll contact you personally in a short time to fix an issue through co-op and prior to any public disclosure.

Feedback

Issues and PRs are welcome. You can provide your feedback or drop us a support request at hello@uploadcare.com.

Generated using TypeDoc