An automated solution to fetch random photos from specific Unsplash users using GitHub Actions and Cloudflare Pages.
With the discontinuation of source.unsplash.com in June 2024, accessing random photos from Unsplash contributors became challenging. This repository provides an alternative method to retrieve random images from your Unsplash portfolio, allowing you to showcase them on your website or GitHub profile.
Every hour, a GitHub Action fetches a random photo from the Unsplash API and generates a Cloudflare Pages redirect file, which is automatically deployed. This ensures that your displayed images are refreshed regularly without manual intervention.
https://unsplash.internetztube.net/internetztube/regular
- Cloudflare Account: Sign up here
- Unsplash Account: Sign up here
- GitHub Account: Sign up here
Follow the steps below to set up the project:
- Visit the Unsplash Developer Applications page.
- Click on "New Application" and provide the necessary details.
- After creation, note down your Access Key for later use.
- Click the "Fork" button in the top-right corner of this repository to create a copy under your GitHub account.
- Navigate to your forked repository's Settings.
- In the sidebar, select Actions → General.
- Under Workflow permissions, ensure the following are enabled:
- Allow all actions and reusable workflows
- Read repository contents and packages permissions
- In your repository's Settings, go to Secrets and variables → Actions.
- Add the following:
- Secret:
- Name:
UNSPLASH_ACCESS_KEY
- Value: Your Unsplash Access Key from step 1.
- Name:
- Variable:
- Name:
UNSPLASH_USERNAMES
- Value: Your Unsplash username(s). If multiple, enter one username per line.
- Name:
- Optional Variable:
- Name:
FALLBACK_TARGET
- Value: A fallback URL for redirection in case of failure.
- Name:
- Secret:
- Log in to your Cloudflare account and navigate to Pages.
- Click "Create a project" and select "Connect to Git".
- Choose your forked GitHub repository.
- Configure the build settings:
- Framework preset: Select "None".
- Build command: Leave empty.
- Build output directory: Leave empty.
- Click "Save and Deploy".
- Note: The initial deployment may fail due to the empty build directory. This is expected and will resolve after the first GitHub Action run.
- Optional: Add a custom domain by setting up a CNAME record pointing to your Cloudflare Pages domain.
- The GitHub Action is scheduled to run hourly.
- It fetches a random image and updates the
_redirects
file. - Cloudflare Pages automatically deploys the changes.
Access the random image using the following URL structure:
https://<your-cloudflare-pages-domain>/<unsplash-username>/raw
https://<your-cloudflare-pages-domain>/<unsplash-username>/regular
https://<your-cloudflare-pages-domain>/<unsplash-username>/full
...
See _redirects file for all routes.