anjerodev / unsplashjs

Javascript fully typed wrapper for the Unsplash API

Home Page:https://jsr.io/@anjerodev/unsplashjs

Repository from Github https://github.comanjerodev/unsplashjsRepository from Github https://github.comanjerodev/unsplashjs

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 apiUrl property 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 count params is provided or not.
  • Bring back Authentication workflow methods.

About

Javascript fully typed wrapper for the Unsplash API

https://jsr.io/@anjerodev/unsplashjs

License:MIT License


Languages

Language:TypeScript 100.0%