Get a list of download counts for all of Unsplash.
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 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 => {
});