Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
U

unsplash-js

> 编程语言
Open source

Official JavaScript wrapper for the Unsplash API

2.2K stars0 likes3 views
WebsiteGitHub

About

Official JavaScript wrapper for the Unsplash API

Unsplash

Official JavaScript wrapper for the Unsplash API.

Key Links:

  • Before using the Unsplash API, register as a developer.
  • Before using the Unsplash API, read the API Guidelines. Specifically, you must:
    • hotlink images
    • attribute photographers
    • trigger a download when appropriate
  • Once you create an application and have an access key, go try the demos:
    • typescript

Documentation

  • Installation
  • Usage
  • Replacing fetch
  • Types
  • Endpoint examples

Installation

bash
$ npm i --save unsplash-js

# OR

$ yarn add unsplash-js

# OR

$ pnpm add unsplash-js

Usage

Creating an instance

To create an instance, simply provide an Object with your accessKey.

typescript
import { createApi } from "unsplash-js";

// on your node server
const serverApi = createApi({
  accessKey: "MY_ACCESS_KEY",
  //...other fetch options
});

// in the browser
const browserApi = createApi({
  baseUrl: "https://mywebsite.com/unsplash-proxy",
  //...other fetch options
});

NOTE: If you're using unsplash-js in the browser, you must proxy your requests through your server by setting baseUrl. This is to authenticate requests with your Access Key to abide by the API Guideline, and keeps your keys confidential. If baseUrl is set, accessKey is unnecessary and vice-versa.

Making a request

createApi returns an instance whose methods each accept a params object and standard fetch options.

Each method is named after an HTTP verb (GET, POST, DELETE, PUT, PATCH).

typescript
const unsplash = createApi({
  accessKey: "MY_ACCESS_KEY",
});

unsplash.GET("/photos/{assetSlug}", {
  params: { path: { assetSlug: "123" } },
});

Global fetch options can be set on the constructor; per-request fetch options override them:

typescript
const unsplash = createApi({
  accessKey: "MY_ACCESS_KEY",
  // `fetch` options to be sent with every request
  headers: { "X-Custom-Header": "foo" },
});

unsplash.GET("/photos/{assetSlug}", {
  params: { path: { assetSlug: "123" } },
  // `fetch` options to be sent with only this request
  headers: { "X-Custom-Header-2": "bar" },
});

Example: Using an AbortController on a specific request:

typescript
const unsplash = createApi({
  accessKey: "MY_ACCESS_KEY",
});

const controller = new AbortController();
const signal = controller.signal;

unsplash
  .GET("/photos/{assetSlug}", {
    params: { path: { assetSlug: "123" } },
    signal,
  })
  .catch((err) => {
    if (err.name === "AbortError") {
      console.log("Fetch aborted");
    }
  });

controller.abort();

Response

There are two possible outcomes to a request:

  1. You received data: result.data is present and populated.
  2. The server returned an error: result.error is present and contains error details.

If you'd like to inspect the full response, result.response contains the raw Response object. You can use this to get the X-Total header value on feed responses, for example.

…

NOTE: All usual fetch exceptions can get thrown when making requests using this library.

Authentication

When using the accessKey parameter on the createApi constructor, the Authorization header will be automatically set to use public authentication.

If you need to use either the user authentication or dynamic client registration scheme, set the Authorization header on each request according to the requirements of the authentication method you've chosen.

Replacing fetch

This is useful when mocking fetch in tests, targeting an older runtime, or providing a custom fetch implementation.

Fetch can be replaced globally or as an argument to createApi.

typescript
// Globally

// server
import fetch from "node-fetch";
global.fetch = fetch;

// browser
import "whatwg-fetch";
typescript
// As an argument to `createApi`

import { createApi } from "unsplash-js";
import nodeFetch from "node-fetch";

const unsplash = createApi({
  accessKey: "MY_ACCESS_KEY",
  fetch: nodeFetch,
});

See the openapi-fetch documentation for more information.

Types

The types for this library target TypeScript v4.5 and above.

This library is written in TypeScript, which means that even if you are writing plain JavaScript, you can still get useful and accurate type information. We highly recommend that you setup your environment (using an editor such as VSCode) to fully benefit from type hinting:

Request completion

Response completion

Endpoint examples

createApi returns a preconfigured openapi-fetch instance whose methods correspond to the usual HTTP verbs (.GET, .POST, .PUT, .DELETE, .PATCH).

For a full list of Unsplash's public endpoints, see the API documentation.

The endpoints covered here are grouped by category:

  • Search
  • Photos
  • Users
  • Collections
  • Topics

For a full list of all available endpoints and their parameters, see the OpenAPI spec.


.GET("/search/photos", { params: { ... }})

Get a list of photos matching the query. See endpoint docs

Parameters

Parameter Location Type Optional/Required Default Note
query query string Required
page query number Optional 1
per_page query number Optional 10
orientation query "portrait" | "landscape" | "squarish" Optional
content_filter query "low" | "high" Optional "low"
color query string Optional see https://unsplash.com/documentation#search-photos
order_by query "latest" | "relevant" Optional "relevant"
collections query Array Optional
lang query string Optional "en" see https://unsplash.com/documentation#supported-languages

Example

typescript
const { data, error } = await unsplash.GET("/search/photos", {
  params: {
    query: {
      query: "cat",
      page: 1,
      per_page: 10,
      color: "green",
      orientation: "portrait",
    },
  },
});

.GET("/search/users", { params: { ... }})

Get a list of users matching the query. See endpoint docs

Parameters

Parameter Location Type Optional/Required Default
query query string Required
page query number Optional 1
per_page query number Optional 10

Example

typescript
const { data, error } = await unsplash.GET("/search/users", {
  params: {
    query: {
      query: "cat",
      page: 1,
      per_page: 10,
    },
  },
});

.GET("/search/collections", { params: { ... }})

Get a list of collections matching the query. See endpoint docs

Parameters

Parameter Location Type Optional/Required Default
query query string Required
page query number Optional 1
per_page query number Optional 10

Example

typescript
const { data, error } = await unsplash.GET("/search/collections", {
  params: {
    query: {
      query: "cat",
      page: 1,
      per_page: 10,
    },
  },
});

.GET("/photos", { params: { ... }})

Get a single page from the list of all photos. See endpoint docs

Parameters

Parameter Location Type Optional/Required Default
page query number Optional 1
per_page query number Optional 10

Example

typescript
const { data, error } = await unsplash.GET("/photos", {
  params: {
    query: { page: 2, per_page: 15 },
  },
});

.GET("/photos/{assetSlug}", { params: { ... }})

Retrieve a single photo. See endpoint docs

Parameters

Parameter Location Type Optional/Required
assetSlug path string Required

Example

typescript
const { data, error } = await unsplash.GET("/photos/{assetSlug}", {
  params: {
    path: { assetSlug: "mtNweauBsMQ" },
  },
});

.GET("/photos/{assetSlug}/statistics", { params: { ... }})

Retrieve a single photo's stats. See endpoint docs

Parameters

Parameter Location Type Optional/Required Default
assetSlug path string Required
resolution query "days" Optional "days"
quantity query number Optional 30

Example

typescript
const { data, error } = await unsplash.GET("/photos/{assetSl

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScriptfreeimagesphotographyphotos

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言