우선 Unsplash Developer 사이트에 접속해 로그인을 하고, Your app 메뉴로 이동합니다.
New Application을 클릭해 Demo버전의 Application 생성하고, API를 사용할 수 있는 key를 발급 받습니다.

client_id=YOUR_ACCESS_KEY 는 발급 받은 API Key를 적습니다.https://api.unsplash.com/photos/?client_id=YOUR_ACCESS_KEY
GET /photos/randomhttps://api.unsplash.com/photos/random?client_id=YOUR_ACCESS_KEY
count와 이미지 검색 키워드인 query를 사용할 예정입니다.
{
"id": "pXhwzz1JtQU",
"updated_at": "2016-07-10T11:00:01-05:00",
"username": "jimmyexample",
"first_name": "James",
"last_name": "Example",
"twitter_username": "jimmy",
"portfolio_url": null,
"bio": "The user's bio",
"location": "Montreal, Qc",
"total_likes": 20,
"total_photos": 10,
"total_collections": 5,
"followed_by_user": false,
"downloads": 4321,
"uploads_remaining": 4,
"instagram_username": "james-example",
"location": null,
"email": "jim@example.com",
"links": {
"self": "https://api.unsplash.com/users/jimmyexample",
"html": "https://unsplash.com/jimmyexample",
"photos": "https://api.unsplash.com/users/jimmyexample/photos",
"likes": "https://api.unsplash.com/users/jimmyexample/likes",
"portfolio": "https://api.unsplash.com/users/jimmyexample/portfolio"
}
}

npm i axios
// 또는
yarn add axios
// 실행하고자 하는 프로젝트의 상위에 axios 라이브러리를 import 합니다.
import axios from 'axios'
import axios from "axios";
const instance = axios.create({
baseURL: 'https://api.unsplash.com/photos/random',
params: {
client_id: process.env.REACT_APP_MOTIVATION_API_KEY,
count: 30,
}
});
export default instance;
const fetchData = async () => {
const imgData = await axios.get();
};
const fetchData = async () => {
const imgData = await axios.get('', {
params: {
query: { query }
}
});
};
import axios from '../api/axios';
import { useContext, useEffect, useState } from 'react'
import styled from 'styled-components';
import { stateContext } from '../utils/stateContext'
const Background = () => {
const [backImgUrl, setBackImgUrl] = useState("");
const { query } = useContext(stateContext);
// 쿼리 값이 변화가 있을 때마다 fetchData() 를 불러 옵니다.
useEffect(() => {
fetchData();
}, [query]);
// 이미지 크기 조절을 위한 함수
const settleImg = (imgData) => {
let index = 0;
while (true) {
if (imgData[index].width / imgData[index].height > 1) {
setBackImgUrl(imgData[index].urls['full']);
break;
}
index++;
}
}
const fetchData = async () => {
const imgData = await axios.get('', {
params: {
query: { query }
}
});
settleImg(imgData.data);
};
return (
<>
<BackgroundImg
style={{
backgroundImage: `linear-gradient( rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7) )`,
backgroundImage: `linear-gradient( rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7) ), url(${backImgUrl})`,
backgroundPosition: "top center",
backgroundSize: "cover",
}} />
</>
)
}
export default Background;
const BackgroundImg = styled.div`
background-size:cover;
background-repeat: no-repeat;
position: absolute;
max-height: 100%;
margin:0;
padding:0;
width: 100vw;
height: 100vh;
z-index: -1;
opacity: 0.8;
`