Socket
Socket
Sign inDemoInstall

unsplash-js

Package Overview
Dependencies
Maintainers
2
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unsplash-js

A Universal JavaScript wrapper for the Unsplash API


Version published
Weekly downloads
61K
increased by12.11%
Maintainers
2
Weekly downloads
 
Created
Source

Unsplash

npm Travis Coveralls

A Universal JavaScript wrapper for the Unsplash API.

Browser Support

ChromeFirefoxSafariOperaIE
Latest ✔Latest ✔Latest ✔Latest ✔10+ ✔

Documentation

Installation

$ npm i --save unsplash-js

Dependencies

This library depends on fetch to make requests to the Unsplash API. For environments that don't support fetch, you'll need to provide a polyfill.

Usage

Creating an instance

To create an instance, simply provide an Object with your applicationId, secret and callbackUrl.

import Unsplash from 'unsplash-js';

const unsplash = new Unsplash({
  applicationId: "{APP_ID}",
  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({
  applicationId: "{APP_ID}",
  secret: "{APP_SECRET}",
  callbackUrl: "{CALLBACK_URL}",
  bearerToken: "{USER_BEARER_TOKEN}"
});

Credentials can be obtained from Unsplash Developers.

React Native

For use with React Native, import from unsplash-js/native instead.

import Unsplash from 'unsplash-js/native';

Authorization workflow

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.

// The OAuth code will be passed to your callback url as a querystring

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 Documentation.


Error handling

unsplash.users.profile("naoufal")
  .catch(err => {
    // Your flawless error handling code
  });

Instance Methods

auth.getAuthenticationUrl(scopes)

Build an OAuth url with requested scopes.

Arguments

ArgumentTypeOpt/RequiredDefault
scopesArrayOptional["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

ArgumentTypeOpt/Required
codestringRequired

Example

unsplash.auth.userAuthentication("{OAUTH_CODE}")
  .then(toJson)
  .then(json => {
    // Your code
  });

auth.setBearerToken(accessToken)

Set a bearer token on the instance.

Arguments

ArgumentTypeOpt/Required
accessTokenstringRequired

Example

unsplash.auth.setBearerToken("{BEARER_TOKEN}");

currentUser.profile()

Get the user’s profile.

Arguments

N/A

Example

unsplash.currentUser.profile()
  .then(toJson)
  .then(json => {
    // Your code
  });

currentUser.updateProfile(options)

Update the current user’s profile.

Arguments

ArgumentTypeOpt/RequiredNotes
optionsObjectRequiredObject 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 => {
    // Your code
  });

users.profile(username)

Retrieve public details on a given user.

Arguments

ArgumentTypeOpt/Required
usernamestringRequired

Example

unsplash.users.profile("naoufal")
  .then(toJson)
  .then(json => {
    // Your code
  });

users.photos(username, page, perPage, orderBy)

Get a list of photos uploaded by a user.

Arguments

ArgumentTypeOpt/RequiredNotes
usernamestringRequired
pagenumberOptional
perPagenumberOptional
orderBystringOptionallatest, popular or oldest

Example

unsplash.users.photos("naoufal", "popular")
  .then(toJson)
  .then(json => {
    // Your code
  });

users.likes(username, page, perPage, orderBy)

Get a list of photos liked by a user.

Arguments

ArgumentTypeOpt/RequiredNotes
usernamestringRequired
pagenumberOptional
perPagenumberOptional
orderBystringOptionallatest, popular or oldest

Example

unsplash.users.likes("naoufal", 2, 15, "popular")
  .then(toJson)
  .then(json => {
    // Your code
  });

users.collections(username, page, perPage)

Get a list of collections created by the user.

Arguments

ArgumentTypeOpt/RequiredNotes
usernamestringRequired
pagenumberOptional
perPagenumberOptional

Example

unsplash.users.collections("naoufal", 2, 15)
  .then(toJson)
  .then(json => {
    // Your code
  });

photos.listPhotos(page, perPage, orderBy)

Get a single page from the list of all photos.

Arguments

ArgumentTypeOpt/Required
pagenumberOptional
perPagenumberOptional
orderBystringOptional

Example

unsplash.photos.listPhotos(2, 15, "latest")
  .then(toJson)
  .then(json => {
    // Your code
  });

photos.listCuratedPhotos(page, perPage, orderBy)

Get a single page from the list of the curated photos.

Arguments

ArgumentTypeOpt/Required
pagenumberOptional
perPagenumberOptional
orderBystringOptional

Example

unsplash.photos.listCuratedPhotos(2, 15, "latest")
  .then(toJson)
  .then(json => {
    // Your code
  });

photos.searchPhotos(query, category, page, perPage)

Get a single page from a photo search. Optionally limit your search to a set of categories by supplying the category ID’s.

Arguments

ArgumentTypeOpt/Required
querystringOptional
categoryArrayOptional
pagenumberOptional
perPagenumberOptional

Example

unsplash.photos.searchPhotos("cats", [11, 88], 1, 15)
  .then(toJson)
  .then(json => {
    // Your code
  });

photos.getPhoto(id, width, height, rectangle)

Retrieve a single photo.

Arguments

ArgumentTypeOpt/Required
idstringRequired
widthnumberOptional
heightnumberOptional
rectangleArrayOptional

Example

unsplash.photos.getPhoto("mtNweauBsMQ", 1920, 1080, [0, 0, 1920, 1080])
  .then(toJson)
  .then(json => {
    // Your code
  });

photos.getPhotoStats(id)

Retrieve a single photo's stats.

Arguments

ArgumentTypeOpt/Required
idstringRequired

Example

unsplash.photos.getPhotoStats("mtNweauBsMQ")
  .then(toJson)
  .then(json => {
    // Your code
  });

Retrieve a single random photo, given optional filters.

When using this function, It is recommended to double check the types of the parameters, in particular for the parameters of type Array.

The category parameter is deprecated and should not be used.

Arguments

ArgumentTypeOpt/Required
widthnumberOptional
heightnumberOptional
querystringOptional
usernamestringOptional
featuredbooleanOptional
categoryArrayOptional
collectionsArrayOptional
countstringOptional

Example

unsplash.photos.getRandomPhoto({ username: "naoufal" })
  .then(toJson)
  .then(json => {
    // Your code
  });

photos.uploadPhoto(photo)

Upload a photo on behalf of the logged-in user. This requires the write_photos scope.

Arguments

ArgumentTypeOpt/Required
photoImage BinaryRequired

Example

import { createReadStream } from "fs";

unsplash.photos.uploadPhoto(createReadStream(__dirname + "path/to/image"))
  .then(toJson)
  .then(json => {
    // Your code
  });

photos.likePhoto(id)

Like a photo on behalf of the logged-in user. This requires the write_likes scope.

Arguments

ArgumentTypeOpt/Required
idstringRequired

Example

unsplash.photos.likePhoto("mtNweauBsMQ")
  .then(toJson)
  .then(json => {
    // Your code
  });

photos.unlikePhoto(id)

Remove a user’s like of a photo.

Arguments

ArgumentTypeOpt/Required
idstringRequired

Example

unsplash.photos.unlikePhoto("mtNweauBsMQ")
  .then(toJson)
  .then(json => {
    // Your code
  });

categories.listCategories()

Get a list of all photo categories.

Arguments

N/A

Example

unsplash.categories.listCategories()
  .then(toJson)
  .then(json => {
    // Your code
  });

categories.category(id)

Retrieve a single category.

Arguments

ArgumentTypeOpt/Required
idstringRequired

Example

unsplash.categories.category(4)
  .then(toJson)
  .then(json => {
    // Your code
  });

categories.categoryPhotos(id, page, perPage)

Retrieve a single category’s photos.

Arguments

ArgumentTypeOpt/Required
idstringRequired
pagenumberOptional
perPagenumberOptional

Example

unsplash.categories.categoryPhotos(4, 3, 15)
  .then(toJson)
  .then(json => {
    // Your code
  });

collections.listCollections(page, perPage, orderBy)

Get a single page from the list of all collections.

Arguments

ArgumentTypeOpt/RequiredNotes
pagenumberOptional
perPagenumberOptional
orderBystringOptionallatest, popular or oldest

Example

unsplash.collections.listCollections(1, 10, "popular")
  .then(toJson)
  .then(json => {
    // Your code
  });

collections.listCuratedCollections(page, perPage)

Get a single page from the list of curated collections.

Arguments

ArgumentTypeOpt/Required
pagenumberOptional
perPagenumberOptional

Example

unsplash.collections.listCuratedCollections(1, 10)
  .then(toJson)
  .then(json => {
    // Your code
  });

collections.listFeaturedCollections(page, perPage)

Get a single page from the list of featured collections.

Arguments

ArgumentTypeOpt/Required
pagenumberOptional
perPagenumberOptional

Example

unsplash.collections.listFeaturedCollections(1, 10)
  .then(toJson)
  .then(json => {
    // Your code
  });

collections.getCollection(id)

Retrieve a single collection. To view a user’s private collections, the read_collections scope is required.

Arguments

ArgumentTypeOpt/Required
idnumberRequired

Example

unsplash.collections.getCollection(123456)
  .then(toJson)
  .then(json => {
    // Your code
  });

collections.getCuratedCollection(id)

Or, for a curated collection:

Arguments

ArgumentTypeOpt/Required
idnumberRequired

Example

unsplash.collections.getCuratedCollection(88)
  .then(toJson)
  .then(json => {
    // Your code
  });

collections.getCollectionPhotos(id, orderBy)

Retrieve a collection’s photos.

Arguments

ArgumentTypeOpt/RequiredNotes
idnumberRequired
pagenumberOptional
perPagenumberOptional
orderBystringOptionallatest, popular or oldest

Example

unsplash.collections.getCollectionPhotos(123456, 1, 10, "popular")
  .then(toJson)
  .then(json => {
    // Your code
  });

collections.getCuratedCollectionPhotos(id, orderBy)

Or, for a curated collection:

Arguments

ArgumentTypeOpt/RequiredNotes
idnumberRequired
pagenumberOptional
perPagenumberOptional
orderBystringOptionallatest, popular or oldest

Example

unsplash.collections.getCuratedCollectionPhotos(88, 1, 10, "popular")
  .then(toJson)
  .then(json => {
    // Your code
  });

collections.createCollection(title, description, private)

Create a new collection. This requires the write_collections scope.

Arguments

ArgumentTypeOpt/Required
titlestringRequired
descriptionstringOptional
privatebooleanOptional

Example

unsplash.collections.createCollection("Birds", "Wild birds from 'round the world", true)
  .then(toJson)
  .then(json => {
    // Your code
  });

collections.updateCollection(id, title, description, private)

Update an existing collection belonging to the logged-in user. This requires the write_collections scope.

Arguments

ArgumentTypeOpt/Required
idnumberRequired
titlestringOptional
descriptionstringOptional
privatebooleanOptional

Example

unsplash.collections.updateCollection(12345, "Wild Birds", "Wild birds from around the world", false)
  .then(toJson)
  .then(json => {
    // Your code
  });

collections.deleteCollection(id)

Delete a collection belonging to the logged-in user. This requires the write_collections scope.

Arguments

ArgumentTypeOpt/Required
idnumberRequired

Example

unsplash.collections.deleteCollection(88)
  .then(toJson)
  .then(json => {
    // Your code
  });

collections.addPhotoToCollection(collectionId, photoId)

Add a photo to one of the logged-in user’s collections. Requires the write_collections scope.

Arguments

ArgumentTypeOpt/Required
collectionIdnumberRequired
photoIdstringRequired

Example

unsplash.collections.addPhotoToCollection(88, 'abc1234')
  .then(toJson)
  .then(json => {
    // Your code
  });

collections.removePhotoFromCollection(collectionId, photoId)

Remove a photo from one of the logged-in user’s collections. Requires the write_collections scope.

Arguments

ArgumentTypeOpt/Required
collectionIdnumberRequired
photoIdstringRequired

Example

unsplash.collections.removePhotoFromCollection(88, 'abc1234')
  .then(toJson)
  .then(json => {
    // Your code
  });

Keywords

FAQs

Package last updated on 13 Mar 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc