Official JavaScript wrapper for the Unsplash API
Official JavaScript wrapper for the Unsplash API
Official JavaScript wrapper for the Unsplash API.
Key Links:
$ npm i --save unsplash-js
# OR
$ yarn add unsplash-js
# OR
$ pnpm add unsplash-jsTo create an instance, simply provide an Object with your accessKey.
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.
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).
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:
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:
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();There are two possible outcomes to a request:
result.data is present and populated.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.
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.
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.
// Globally
// server
import fetch from "node-fetch";
global.fetch = fetch;
// browser
import "whatwg-fetch";// 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.
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:
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:
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
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
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
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
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
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
const { data, error } = await unsplash.GET("/photos/{assetSlNo open issues yet, or sync has not completed.