collections.listCollections(page, perPage, orderBy)
Get a single page from the list of all collections. See endpoint docs π
Arguments
Argument | Type | Opt/Required | Notes |
---|
page | number | Optional | |
perPage | number | Optional | |
orderBy | string | Optional | latest , oldest |
Example
unsplash.collections.listCollections(1, 10, "latest")
.then(toJson)
.then(json => {
});
collections.getCollection(id)
Retrieve a single collection. To view a userβs private collections, the read_collections
scope is required. See endpoint docs π
Arguments
Argument | Type | Opt/Required |
---|
id | number | Required |
Example
unsplash.collections.getCollection(123456)
.then(toJson)
.then(json => {
});
collections.getCollectionPhotos(id, page, perPage, orderBy, options)
Retrieve a collectionβs photos. See endpoint docs π
Arguments
Argument | Type | Opt/Required | Notes |
---|
id | number | Required | |
page | number | Optional | |
perPage | number | Optional | |
orderBy | string | Optional | latest , oldest |
options | object | Optional | |
options.orientation | string | Optional | landscape , portrait , squarish |
Example
unsplash.collections.getCollectionPhotos(123456, 1, 10, "latest")
.then(toJson)
.then(json => {
});
collections.createCollection(title, description, private)
Create a new collection. This requires the write_collections
scope. See endpoint docs π
Arguments
Argument | Type | Opt/Required |
---|
title | string | Required |
description | string | Optional |
private | boolean | Optional |
Example
unsplash.collections.createCollection("Birds", "Wild birds from 'round the world", true)
.then(toJson)
.then(json => {
});
collections.updateCollection(id, title, description, private)
Update an existing collection belonging to the logged-in user. This requires the write_collections
scope. See endpoint docs π
Arguments
Argument | Type | Opt/Required |
---|
id | number | Required |
title | string | Optional |
description | string | Optional |
private | boolean | Optional |
Example
unsplash.collections.updateCollection(12345, "Wild Birds", "Wild birds from around the world", false)
.then(toJson)
.then(json => {
});
collections.deleteCollection(id)
Delete a collection belonging to the logged-in user. This requires the write_collections
scope. See endpoint docs π
Arguments
Argument | Type | Opt/Required |
---|
id | number | Required |
Example
unsplash.collections.deleteCollection(88)
.then(toJson)
.then(json => {
});
collections.addPhotoToCollection(collectionId, photoId)
Add a photo to one of the logged-in userβs collections. Requires the write_collections
scope. See endpoint docs π
Arguments
Argument | Type | Opt/Required |
---|
collectionId | number | Required |
photoId | string | Required |
Example
unsplash.collections.addPhotoToCollection(88, 'abc1234')
.then(toJson)
.then(json => {
});
collections.removePhotoFromCollection(collectionId, photoId)
Remove a photo from one of the logged-in userβs collections. Requires the write_collections
scope. See endpoint docs π
Arguments
Argument | Type | Opt/Required |
---|
collectionId | number | Required |
photoId | string | Required |
Example
unsplash.collections.removePhotoFromCollection(88, 'abc1234')
.then(toJson)
.then(json => {
});
collections.listRelatedCollections(collectionId)
Lists collections related to the provided one. See endpoint docs π
Arguments
Argument | Type | Opt/Required |
---|
collectionId | number | Required |
Example
unsplash.collections.listRelatedCollections(88)
.then(toJson)
.then(json => {
});
Note: Most endpoints do not need to be authenticated by an individual user to be accessed and can instead be accessed with public authentication. Endpoints that require user authentication will be explicitly marked with the required scopes.
When initializing an instance of Unsplash, you'll need to include your application's secretKey
and callbackUrl
as defined in the API documentation:
const unsplash = new Unsplash({
accessKey: "{APP_ACCESS_KEY}",
secret: "{APP_SECRET}",
callbackUrl: "{CALLBACK_URL}"
});
If you already have a bearer token, you can also provide it to the constructor:
const unsplash = new Unsplash({
accessKey: "{APP_ACCESS_KEY}",
secret: "{APP_SECRET}",
callbackUrl: "{CALLBACK_URL}",
bearerToken: "{USER_BEARER_TOKEN}"
});
Generate an authentication url with the scopes your app requires.
const authenticationUrl = unsplash.auth.getAuthenticationUrl([
"public",
"read_user",
"write_user",
"read_photos",
"write_photos"
]);
Now that you have an authentication url, you'll want to redirect the user to it.
location.assign(authenticationUrl);
After the user authorizes your app she'll be redirected to your callback url with a code
querystring present. Request an access token using that code.
unsplash.auth.userAuthentication(query.code)
.then(toJson)
.then(json => {
unsplash.auth.setBearerToken(json.access_token);
});
For more information on the authroization workflow, consult the Unsplash API Documentation.
auth.getAuthenticationUrl(scopes)
Build an OAuth url with requested scopes.
Arguments
Argument | Type | Opt/Required | Default |
---|
scopes | Array | Optional | ["public"] |
Example
const authenticationUrl = unsplash.auth.getAuthenticationUrl([
"public",
"read_user",
"write_user",
"read_photos",
"write_photos"
]);
auth.userAuthentication(code)
Retrieve a user's access token.
Arguments
Argument | Type | Opt/Required |
---|
code | string | Required |
Example
unsplash.auth.userAuthentication("{OAUTH_CODE}")
.then(toJson)
.then(json => {
});
auth.setBearerToken(accessToken)
Set a bearer token on the instance.
Arguments
Argument | Type | Opt/Required |
---|
accessToken | string | Required |
Example
unsplash.auth.setBearerToken("{BEARER_TOKEN}");
currentUser.profile()
Get the userβs profile.
Arguments
N/A
Example
unsplash.currentUser.profile()
.then(toJson)
.then(json => {
});
currentUser.updateProfile(options)
Update the current userβs profile.
Arguments
Argument | Type | Opt/Required | Notes |
---|
options | Object | Required | Object with the following optional keys: username , firstName , lastName , email , url , location , bio , instagramUsername |
Example
unsplash.currentUser.updateProfile({
username: "drizzy",
firstName: "Aubrey",
lastName: "Graham",
email: "drizzy@octobersveryown.com",
url: "http://octobersveryown.com",
location: "Toronto, Ontario, Canada",
bio: "Views from the 6",
instagramUsername: "champagnepapi"
})
.then(toJson)
.then(json => {
});
Helpers
toJson(res)
Arguments
Argument | Type | Opt/Required |
---|
res | Object | Required |
Example
import Unsplash, { toJson } from "unsplash-js";
const unsplash = new Unsplash({
accessKey: "{YOUR_ACCESS_KEY}",
secret: "{YOUR_SECRET_KEY}"
});
unsplash.stats.total()
.then(toJson)
.then(json => {
});