@anjerodev/unsplashjs@0.2.0
Unofficial Javascript fully typed wrapper for the Unsplash API.
Unofficial Unsplash SDK
Javascript fully typed wrapper for the Unsplash API.
Why this?
For some unknown reason, the official Unsplash Javascript SDK has been archived, and it is quite outdated and/or incomplete.
With this SDK, I intend to provide a more complete and up-to-date wrapper for the Unsplash API.
The library is based in the official, but with some key changes in the response handling, which always will returns an object with the properties data, error and total.
Installation
This is a Deno package, but it can be used in Node.js as well.
You can find the installation commands for every package manager in the jsr package documentation.
Usage
Declare the client
To ensure your access keys remain confidential, the API client should be configured and run on the server. Refer to Obtaining an Unsplash Access Key for details on acquiring the key.
As mention in the official documentation:
NOTE: If you're using the SDK publicly in the browser, you'll need to proxy your requests through your server to sign the requests with the Access Key to abide by the API Guideline to keep keys confidential. We provide an
apiUrlproperty that lets you do so. You should only need to provide one of those two values in any given scenario.
import UnsplashClient from "unsplashjs"; // On the server const unsplash = new UnsplashClient({ accessKey: "YOUR_ACCESS_KEY", //...other options }); // On the client const unsplash = new UnsplashClient({ apiUrl: "https://mywebsite.com/unsplash-proxy", //...other options });
Making a request
import UnsplashClient from "unsplashjs"; // On the server const unsplash = new UnsplashClient({ accessKey: "YOUR_ACCESS_KEY", }); unsplash.photos.get({ photo_id: "123" }).then((result) => { const { data, error, total } = result; if (error) { console.error(error); // Handle the error return; } // Do something with the data console.log({ data }); });
Obtaining an Unsplash Access Key
Create an account on Unsplash, and then on your Developer dashboard, create a new application.
What this library adds to the official Unsplash API
- More complete and updated types.
- Automatic type inference for getRandom photos depending if
countparams is provided or not. - Bring back Authentication workflow methods.
Add Package
deno add jsr:@anjerodev/unsplashjs
Import symbol
import * as unsplashjs from "@anjerodev/unsplashjs";
Import directly with a jsr specifier
import * as unsplashjs from "jsr:@anjerodev/unsplashjs";
Add Package
pnpm i jsr:@anjerodev/unsplashjs
pnpm dlx jsr add @anjerodev/unsplashjs
Import symbol
import * as unsplashjs from "@anjerodev/unsplashjs";
Add Package
yarn add jsr:@anjerodev/unsplashjs
yarn dlx jsr add @anjerodev/unsplashjs
Import symbol
import * as unsplashjs from "@anjerodev/unsplashjs";
Add Package
vlt install jsr:@anjerodev/unsplashjs
Import symbol
import * as unsplashjs from "@anjerodev/unsplashjs";
Add Package
npx jsr add @anjerodev/unsplashjs
Import symbol
import * as unsplashjs from "@anjerodev/unsplashjs";
Add Package
bunx jsr add @anjerodev/unsplashjs
Import symbol
import * as unsplashjs from "@anjerodev/unsplashjs";