Highlights:
- Synchronous + asynchronous API support
- Java 11+ (compatible with all JVM languages: Kotlin, Scala, Groovy, Clojure, etc.)
- Faithful port of the wrapsplash npm module
LatteSplash is a production-ready API wrapper for the popular Unsplash platform, written in Java 11.
Unsplash provides beautiful high quality free images and photos that you can download and use for any project without any attribution.
Before using the Unsplash API, you need to register as a developer and read the API Guidelines.
Note: Every application must abide by the API Guidelines. Specifically, remember to hotlink images and trigger a download when appropriate.
- About
- Installation
- Sample Usage
- Development
- Feature Parity with wrapsplash
- Dependency
- API Documentation
- Schema
- Authorization
- Users APIs
- Photos APIs
- Search APIs
- Current User APIs
- Stats APIs
- Collections APIs
- Link Relations
- List Collections
- List Featured Collections
- List Curated Collections
- Get a Collection
- Get a Curated Collection
- Get a Collection's Photos
- Get a Curated Collection's Photos
- List a Collection's Related Collections
- Create a New Collection
- Update an Existing Collection
- Delete a Collection
- Add a Photo to a Collection
- Remove a Photo from a Collection
- Continuous Integration (CI)
- Tests
- License
- Acknowledgements
<dependency>
<groupId>com.lattesplash</groupId>
<artifactId>lattesplash</artifactId>
<version>1.0.0</version>
</dependency>implementation 'com.lattesplash:lattesplash:1.0.0'import com.lattesplash.LatteSplash;
import com.lattesplash.LatteSplashConfig;
import com.lattesplash.LatteSplashResponse;
// Initialize with bearer token
LatteSplashConfig config = new LatteSplashConfig.Builder()
.bearerToken("<bearer-token>")
.build();
LatteSplash unsplash = new LatteSplash(config);
// Synchronous
LatteSplashResponse result = unsplash.photos().getPhoto("<photo-id>");
System.out.println(result.getData());
// Asynchronous
unsplash.photos().getPhotoAsync("<photo-id>")
.thenAccept(response -> System.out.println(response.getData()))
.exceptionally(error -> {
System.err.println(error.getMessage());
return null;
});mvn compile
mvn test
mvn package # Build the JAR
mvn javadoc:javadoc # Generate JavadocThis Java port supports all features of the original wrapsplash npm module:
| Feature | Status |
|---|---|
| Bearer token authentication | Supported |
| Client-ID authentication | Supported |
| All 34 API methods | Supported |
Sync + Async APIs (CompletableFuture) |
Supported |
| Input validation | Supported |
| Retry with configurable delay | Supported |
| Configurable timeout | Supported |
| SHA-256 header hashing | Supported |
Error normalization (LatteSplashError) |
Supported |
| 204 Content Deleted handling | Supported |
| 403 Rate Limit handling | Supported |
This library depends on OkHttp and Gson to make requests and handle JSON serialization for the Unsplash API. It uses SLF4J for logging.
Note: This library uses the SLF4J API for logging but does not include a logging implementation. You must provide an SLF4J binding (e.g., Logback, Log4j2, or SLF4J Simple) in your application's classpath. Without a binding, you will see a warning at runtime and no log output will be produced.
The API we are using is https://api.unsplash.com/. Responses are sent as JSON.
When retrieving a list of objects, an abbreviated or summary version of that object is returned - i.e., a subset of its attributes. To get a full detailed version of that object, fetch it individually.
If an error occurs, whether on the server or client side, the error message(s) will be returned in an errors array.
For example:
422 Unprocessable Entity{
"errors": ["Username is missing", "Password cannot be blank"]
}Many actions can be performed without requiring authentication from a specific user. For example, downloading a photo does not require a user to log in.
To authenticate requests in this way, pass your application's access key via the HTTP Authorization header:
Authorization: Client-ID YOUR_ACCESS_KEYYou can also pass this value using a client_id query parameter:
https://api.unsplash.com/photos/?client_id=YOUR_ACCESS_KEYIf only your access key is sent, attempting to perform non-public actions that require user authorization will result in a 401 Unauthorized response.
The Unsplash API uses OAuth2 to authenticate and authorize Unsplash users. Unsplash's OAuth2 paths live at https://unsplash.com/oauth/.
Before using LatteSplash:
- Developers are required to create a developer account from Unsplash.
- Create a new App from Your Apps page.
- Get the
Access Key,Secret key,Callback URLs, andAuthorization code. - If you have a Bearer Token, then its super, or else you can generate it using LatteSplash.
Note:
Authorization codecan be obtained by clicking theAuthorizelink next toCallback URLs. AlsoAuthorization codeis a one-time use code, you have to generate it again, if the action fails!.
LatteSplash instance is created by passing configuration obtained from Unsplash developer account. The configuration is built using the LatteSplashConfig.Builder class. The following example shows all the available options.
LatteSplashConfig config = new LatteSplashConfig.Builder()
.accessKey("<api-key>")
.secretKey("<secret-key>")
.redirectUri("<callback-url>")
.code("<authorization-code>")
.timeout(10000) // optional, default: 10000ms
.retries(2) // optional, default: 2
.retryDelayMs(100) // optional, default: 100ms
.build();
LatteSplash unsplash = new LatteSplash(config);If you have a bearer_token, then only bearer token has to be passed in.
LatteSplashConfig config = new LatteSplashConfig.Builder()
.bearerToken("<bearer-token>")
.build();
LatteSplash unsplash = new LatteSplash(config);A method to generate a Bearer Token for write_access to private data.
The constructor in this case requires accessKey, secretKey, redirectUri, and code to generate bearer token.
Note: No Parameters are required for this function.
LatteSplashConfig config = new LatteSplashConfig.Builder()
.accessKey("<api-key>")
.secretKey("<secret-key>")
.redirectUri("<callback-url>")
.code("<authorization-code>")
.build();
LatteSplash unsplash = new LatteSplash(config);
// Synchronous
LatteSplashResponse result = unsplash.generateBearerToken();
System.out.println(result.getData());
// Asynchronous
unsplash.generateBearerTokenAsync()
.thenAccept(response -> System.out.println(response.getData()))
.exceptionally(error -> {
System.err.println(error.getMessage());
return null;
});If successful, the response body will be a JSON representation of your user's access token a.k.a bearer token:
{
"access_token": "091343ce13c8ae780065ecb3b13dc903475dd22cb78a05503c2e0c69c5e98044",
"token_type": "bearer",
"scope": "public read_photos write_photos",
"created_at": 1436544465
}and once you have your bearer_token you can use it in your app like this:
LatteSplashConfig config = new LatteSplashConfig.Builder()
.bearerToken("<bearer-token>")
.build();
LatteSplash unsplash = new LatteSplash(config);A method to retrieve public details on a given user.
GET /users/:username
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| username | String | The username of the particular user | no | |
| width | Integer | Width of the profile picture in pixels | yes | |
| height | Integer | Height of the profile picture in pixels | yes |
Note: When optional height & width are specified the profile image will be included in the "profile_image" object as "custom".
unsplash.users().getPublicProfile("<username>", 600, 600);A method to retrieve a single user's portfolio link.
GET /users/:username/portfolio
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| username | String | The username of the particular user | no |
unsplash.users().getUserPortfolio("<username>");A method to get a list of photos uploaded by a particular user.
GET /users/:username/photos
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| username | String | The username of the particular user | no | |
| page | Integer | Page number to retrieve | yes | 1 |
| perPage | Integer | Number of items per page | yes | 10 |
| stats | Boolean | Show the stats for each user's photo | yes | false |
| resolution | String | The frequency of the stats | yes | days |
| quantity | Integer | The amount of for each stat | yes | 30 |
| orderBy | String | How to sort the photos.(Valid values: latest, oldest, popular) |
yes | latest |
unsplash.users().getUserPhotos("<username>", 1, 10, false, "days", 30, "latest");A method to get a list of photos liked by a user.
GET /users/:username/likes
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| username | String | The username of the particular user | no | |
| page | Integer | Page number to retrieve | yes | 1 |
| perPage | Integer | Number of items per page | yes | 10 |
| orderBy | String | How to sort the photos.(Valid values: latest, oldest, popular) |
yes | latest |
unsplash.users().getUserLikedPhotos("<username>", 1, 10, "latest");A method to get a list of collections created by the user.
GET /users/:username/collections
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| username | String | The username of the particular user | no | |
| page | Integer | Page number to retrieve | yes | 1 |
| perPage | Integer | Number of items per page | yes | 10 |
unsplash.users().getUserCollections("<username>", 1, 10);A method to get a user's account statistics.
GET /users/:username/statistics
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| username | String | The username of the particular user | no | |
| resolution | String | The frequency of the stats | yes | days |
| quantity | Integer | The amount of for each stat | yes | 30 |
unsplash.users().getUserStatistics("<username>", "days", 30);A method to get a single page from the list of all photos.
GET /photos
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| page | Integer | Page number to retrieve | yes | 1 |
| perPage | Integer | Number of items per page | yes | 10 |
| orderBy | String | How to sort the photos.(Valid values: latest, oldest, popular) |
yes | latest |
unsplash.photos().listPhotos(1, 10, "latest");A method to get a single page from the list of the curated photos.
GET /photos/curated
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| page | Integer | Page number to retrieve | yes | 1 |
| perPage | Integer | Number of items per page | yes | 10 |
| orderBy | String | How to sort the photos.(Valid values: latest, oldest, popular) |
yes | latest |
unsplash.photos().listCuratedPhotos(1, 10, "latest");A method to retrieve a single photo.
GET /photos/:id
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| id | String | The photo's ID | no | |
| width | Integer | Image width in pixels | yes | |
| height | Integer | Image height in pixels | yes | |
| rect | String | 4 comma-separated integers representing x, y, width, height of the cropped rectangle | yes |
Note: Supplying the optional width or height parameters will result in the custom photo URL being added to the urls object:
unsplash.photos().getPhoto("<id of the photo>", 500, 500, "x, y, width, height");A method to retrieve a single random photo, given optional filters.
GET /photos/random
Note: All parameters are optional, and can be combined to narrow the pool of photos from which a random one will be chosen.
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| collections | String | The public collection ID('s) to filter selection. If multiple, comma-separated | yes | |
| featured | Boolean | Limit selection to featured photos | yes | false |
| username | String | Limit selection to a single user | yes | |
| query | String | Limit selection to photos matching a search term | yes | |
| width | Integer | The Image width in pixels | yes | |
| height | Integer | The Image height in pixels | yes | |
| orientation | String | Filter search results by photo orientation. (Valid values are landscape, portrait, and squarish) |
yes | landscape |
| count | Integer | The number of photos to return. (max: 30) |
yes | 1 |
Note: You can't use the collections and query parameters in the same request. When supplying a count parameter - and only then - the response will be an array of photos, even if the value of count is 1.
unsplash.photos().getRandomPhoto(null, false, null, null, null, null, "landscape", 1);A method to retrieve total number of downloads, views and likes of a single photo, as well as the historical breakdown of these stats in a specific timeframe (default is 30 days).
GET /photos/:id/statistics
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| id | String | The photo's ID | no | |
| resolution | String | The frequency of the stats | yes | days |
| quantity | Integer | The amount of for each stat | yes | 30 |
Note: Currently, the only resolution param supported is "days". The quantity param can be any number between 1 and 30.
unsplash.photos().getPhotoStatistics("<photo-id>", "days", 10);A method to retrieve a single photo's download link. Preferably hit this endpoint if a photo is downloaded in your application for use (example: to be displayed on a blog article, to be shared on social media, to be remixed, etc).
GET /photos/:id/download
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| id | String | The photo's ID | no |
Note: This is different than the concept of a view, which is tracked automatically when you hotlink an image.
unsplash.photos().getPhotoLink("<photo-id>");A method to update a photo on behalf of the logged-in user. This requires the write_photos scope and bearer_token.
PUT /photos/:id
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| id | String | The photo's ID | no | |
| location | Map | The location object holding location data | yes | |
| exif | Map | The exif object holding exif data | yes |
Note: Exchangeable image file format (officially Exif, according to JEIDA/JEITA/CIPA specifications) is a standard that specifies the formats for images, sound, and ancillary tags used by digital cameras (including smartphones), scanners and other systems handling image and sound files recorded by digital cameras. Readmore
| map[key] | Description |
|---|---|
| location["latitude"] | The photo location's latitude (Optional) |
| location["longitude"] | The photo location's longitude (Optional) |
| location["name"] | The photo location's name (Optional) |
| location["city"] | The photo location's city (Optional) |
| location["country"] | The photo location's country (Optional) |
| location["confidential"] | The photo location's confidentiality (Optional) |
| exif["make"] | Camera's brand (Optional) |
| exif["model"] | Camera's model (Optional) |
| exif["exposure_time"] | Camera's exposure time (Optional) |
| exif["aperture_value"] | Camera's aperture value (Optional) |
| exif["focal_length"] | Camera's focal length (Optional) |
| exif["iso_speed_ratings"] | Camera's iso (Optional) |
Map<String, Object> location = Map.of("country", "INDIA");
Map<String, Object> exif = Map.of("make", "Redmi Note 3");
unsplash.photos().updatePhoto("<photo-id>", location, exif);A method to like a photo on behalf of the logged-in user. This requires the write_likes scope.
POST /photos/:id/like
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| id | String | The photo's ID | no |
Note: This action is idempotent; sending the POST request to a single photo multiple times has no additional effect.
unsplash.photos().likePhoto("<photo-id>");A method to remove a user's like of a photo.
DELETE /photos/:id/like
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| id | String | The photo's ID | no |
Note: This action is idempotent; sending the DELETE request to a single photo multiple times has no additional effect.
unsplash.photos().unlikePhoto("<photo-id>");A method to get a single page of photo results for a particular query.
GET /search/photos
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| query | String | The search query | no | |
| page | Integer | Page number to retrieve | yes | 1 |
| perPage | Integer | Number of items per page | yes | 10 |
| collections | String | Collection ID('s) to narrow search. If multiple, comma-separated. | yes | |
| orientation | String | Filter search results by photo orientation. (Valid values are landscape, portrait, and squarish.) |
yes | landscape |
unsplash.search().searchPhotos("cars", 1, 10, "", "landscape");A method to get a single page of collection results for a query.
GET /search/collections
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| query | String | The search query | no | |
| page | Integer | Page number to retrieve | yes | 1 |
| perPage | Integer | Number of items per page | yes | 10 |
unsplash.search().searchCollections("cars", 1, 10);A method to get a single page of user results for a query.
GET /search/users
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| query | String | The search query | no | |
| page | Integer | Page number to retrieve | yes | 1 |
| perPage | Integer | Number of items per page | yes | 10 |
unsplash.search().searchUsers("<search-keyword>", 1, 10);A method to get the current User's profile. To access a user's private data, the user is required to authorize the read_user scope. Without it, this request will return a 403 Forbidden response.
GET /me
Note: No Parameters are required.
Note: Without a Bearer token (i.e. using a
Client-ID token) this request will return a401 Unauthorizedresponse.
unsplash.currentUser().getCurrentUserProfile();A method to update the current User's profile.
PUT /me
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| username | String | The username of the current user | yes | |
| firstName | String | The first name of the current user | yes | |
| lastName | String | The last name of the current user | yes | |
| String | The email id of the current user | yes | ||
| url | String | The Portfolio/personal URL of the current user | yes | |
| location | String | The location of the current user | yes | |
| bio | String | The About/bio of the current user | yes | |
| instagramUsername | String | The Instagram username of the current user | yes |
Note: This action requires the
write_user scope. Without it, it will return a403 Forbidden response.
unsplash.currentUser().updateCurrentUserProfile("<username>", "<first_name>", "<last_name>", "<email>", "<url>", "<location>", "<bio>", "<instagram_username>");A method to get a list of counts for all of Unsplash.
GET /stats/total
unsplash.stats().getStatsTotals();{
"total_stats": {
"photos": 10000,
"downloads": 2000,
"views": 5000,
"likes": 800,
"photographers": 100,
"pixels": 200000,
"downloads_per_second": 10,
"views_per_second": 20,
"developers": 20,
"applications": 50,
"requests": 8000
}
}A method to get the overall Unsplash stats for the past 30 days.
GET /stats/month
unsplash.stats().getStatsMonth();{
"month_stats": {
"downloads": 20,
"views": 200,
"likes": 60,
"new_photos": 10,
"new_photographers": 5,
"new_pixels": 2000,
"new_developers": 8,
"new_applications": 5,
"new_requests": 100
}
}Collections have the following link relations:
| rel | Description |
|---|---|
self |
API location of this collection |
html |
HTML location of this collection |
photos |
API location of this collection's photos |
related |
API location of this collection's related collections (Non-curated collections only) |
download |
Download location of this collection's zip file (Curated collections only) |
A method to get a single page from the list of all collections.
GET /collections
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| page | Integer | Page number to retrieve | yes | 1 |
| perPage | Integer | Number of items per page | yes | 10 |
unsplash.collections().listCollections(null, null);A method to get a single page from the list of featured collections.
GET /collections/featured
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| page | Integer | Page number to retrieve | yes | 1 |
| perPage | Integer | Number of items per page | yes | 10 |
unsplash.collections().listFeaturedCollections(null, null);A method to get a single page from the list of curated collections.
GET /collections/curated
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| page | Integer | Page number to retrieve | yes | 1 |
| perPage | Integer | Number of items per page | yes | 10 |
unsplash.collections().listCuratedCollections(null, null);A method to retrieve a single collection. To view a user's private collections, the read_collections scope is required.
GET /collections/:id
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| id | String | The Collection ID | no |
unsplash.collections().getCollection("<collection-id>");A method to retrieve a single curated collection. To view a user's private collections, the read_collections scope is required.
GET /collections/curated/:id
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| id | String | The Collection ID | no |
unsplash.collections().getCuratedCollection("<curated-collection-id>");A method to retrieve a collection's photos.
GET /collections/:id/photos
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| id | String | The Collection ID | no | |
| page | Integer | Page number to retrieve | yes | 1 |
| perPage | Integer | Number of items per page | yes | 10 |
unsplash.collections().getCollectionPhotos("<collection-id>", 1, 10);A method to retrieve a curated collection's photos.
GET /collections/curated/:id/photos
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| id | String | The Collection ID | no | |
| page | Integer | Page number to retrieve | yes | 1 |
| perPage | Integer | Number of items per page | yes | 10 |
unsplash.collections().getCuratedCollectionPhotos("<curated-collection-id>", 1, 10);A method to retrieve a list of collections related to this one.
GET /collections/:id/related
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| id | String | The Collection ID | no |
unsplash.collections().listRelatedCollections("<collection-id>");A method to create a new collection. This requires the write_collections scope.
POST /collections
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| title | String | The title of the collection | no | |
| description | String | The collection's description | yes | |
| isPrivate | Boolean | Whether to make this collection private | yes | false |
unsplash.collections().createCollection("<collection-name>", "<description>", false);A method to update an existing collection belonging to the logged-in user. This requires the write_collections scope.
PUT /collections/:id
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| id | String | The collection id | no | |
| title | String | The title of the collection | yes | |
| description | String | The collection's description | yes | |
| isPrivate | Boolean | Whether to make this collection private | yes | false |
unsplash.collections().updateCollection("<collection-id>", "<collection-name>", "<description>", false);A method to delete a collection belonging to the logged-in user. This requires the write_collections scope.
DELETE /collections/:id
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| id | String | The Collection ID | no |
unsplash.collections().deleteCollection("<collection-id>");A method to add a photo to one of the logged-in user's collections. Requires the write_collections scope.
POST /collections/:collection_id/add
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| collectionId | String | The Collection ID | no | |
| photoId | String | The Photo ID | no |
Note: If the photo is already in the collection, this action has no effect.
unsplash.collections().addPhotoToCollection("<collection-id>", "<photo-id>");A method to remove a photo from one of the logged-in user's collections. Requires the write_collections scope.
DELETE /collections/:collection_id/remove
| Parameter | Type | Description | Optional | Default |
|---|---|---|---|---|
| collectionId | String | The Collection ID | no | |
| photoId | String | The Photo ID | no |
unsplash.collections().removePhotoFromCollection("<collection-id>", "<photo-id>");This project uses Maven for build and JUnit 5 for testing. In CI, run:
mvn clean compile
mvn test
mvn packageA minimal GitHub Actions workflow is included in .github/workflows/ci.yml for automated build and test on Java 11, 17, and 21.
LatteSplash uses JUnit 5 as the testing framework with Mockito for mocking and OkHttp MockWebServer for HTTP-level tests. Test files are available in the src/test/java/com/lattesplash/ folder. 95 tests covering all API endpoints, error handling, validation, and async behavior.
The MIT License
Copyright (c) 2018- Sandeep Vattapparambil, http://www.sandeepv.in
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Thanks, and Kudos to team Unsplash for creating a wonderful platform for sharing beautiful high quality free images and photos.
Port of wrapsplash npm module by Sandeep Vattapparambil.
