Skip to content

Instantly share code, notes, and snippets.

@ashikka
Created December 30, 2021 16:54
Show Gist options
  • Select an option

  • Save ashikka/a705ac9a34e9577ed5b5c5ef001c8acd to your computer and use it in GitHub Desktop.

Select an option

Save ashikka/a705ac9a34e9577ed5b5c5ef001c8acd to your computer and use it in GitHub Desktop.
import express from "express";
import { createApi } from "unsplash-js";
import nodeFetch from "node-fetch";
const router = express.Router();
router.post("/", async (req, res) => {
try {
const pictureName = req.body.pictureName;
const unsplash = createApi({
accessKey: process.env.ACCESS_KEY,
fetch: nodeFetch,
});
unsplash.search
.getPhotos({ query: pictureName, page: 1, perPage: 10 })
.then((result) => {
if (result.errors) {
res.json({ success: false, message: result.errors[0] });
} else {
res.json({ success: true, message: result.response });
}
});
} catch (e) {
res.json({ success: false, message: e });
}
});
export default router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment