This integration guide is following the Quick Start Guide. We assume that you have fully completed its "Hands-on" path, and therefore can consume the API by browsing this url(opens new window).
If you haven't gone through the Quick Start Guide, the way you request a Strapi API with Next.js(opens new window) remains the same except that you will not fetch the same content.
{"data":[{"id":1,"attributes":{"name":"Biscotte Restaurant","description":"Welcome to Biscotte restaurant! Restaurant Biscotte offers a cuisine based on fresh, quality products, often local, organic when possible, and always produced by passionate producers.","createdAt":"2022-07-31T09:14:12.569Z","updatedAt":"2022-07-31T09:14:12.569Z","publishedAt":"2022-07-31T09:14:12.569Z"}}],"meta":{"pagination":{"page":1,"pageSize":25,"pageCount":1,"total":1}}}
constHome=({ restaurants, error })=>{if(error){return<div>An error occured:{error.message}</div>;}return(<ul>{restaurants.data.map(restaurant=>(<li key={restaurant.id}>{restaurant.attributes.name}</li>))}</ul>);};
Home.getInitialProps=asyncctx=>{try{// Parses the JSON returned by a network requestconstparseJSON=resp=>(resp.json ? resp.json(): resp);// Checks if a network request came back fine, and throws an error if notconstcheckStatus=resp=>{if(resp.status >=200&& resp.status <300){return resp;}returnparseJSON(resp).then(resp=>{throw resp;});};const headers ={'Content-Type':'application/json',};const restaurants =awaitfetch('http://localhost:1337/api/restaurants',{
method:'GET',
headers,}).then(checkStatus).then(parseJSON);return{ restaurants };}catch(error){return{ error };}};exportdefault Home;
Execute a POST request on the restaurant collection type in order to create a restaurant.
Be sure that you activated the create permission for the restaurant collection type and the find permission for the category Collection type.
Add the ?populate=categories query parameter to return categories with the response.
In this example a japanese category has been created which has the id: 3.
Example POST request with axios
import axios from'axios';
axios
.post('http://localhost:1337/api/restaurants/?populate=categories',{
data:{
name:'Dolemon Sushi',
description:'Unmissable Japanese Sushi restaurant. The cheese and salmon makis are delicious',
categories:[3],},}).then(response=>{
console.log(response);});
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Example POST request with fetch
fetch('http://localhost:1337/api/restaurants/?populate=categories',{
method:'POST',
headers:{'Content-Type':'application/json',},
body:JSON.stringify({
data:{
name:'Dolemon Sushi',
description:'Unmissable Japanese Sushi restaurant. The cheese and salmon makis are delicious',
categories:[3],},}),}).then(response=> response.json()).then(data=> console.log(data));
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Example response
{"data":{"id":2,"attributes":{"name":"Dolemon Sushi","description":"Unmissable Japanese Sushi restaurant. The cheese and salmon makis are delicious","createdAt":"2022-07-31T09:14:12.569Z","updatedAt":"2022-07-31T09:14:12.569Z","publishedAt":"2022-07-31T09:14:12.566Z","categories":{"data":[{"id":3,"attributes":{"name":"Japanese","createdAt":"2022-07-31T10:49:03.933Z","updatedAt":"2022-07-31T10:49:04.893Z","publishedAt":"2022-07-31T10:49:04.890Z"}}]}}},"meta":{}}
{"data":{"id":2,"attributes":{"name":"Dolemon Sushi","description":"Unmissable Japanese Sushi restaurant. The cheese and salmon makis are delicious","createdAt":"2022-07-31T09:14:12.569Z","updatedAt":"2022-07-31T11:17:31.598Z","publishedAt":"2022-07-31T09:14:12.566Z","categories":{"data":[{"id":2,"attributes":{"name":"Brunch","createdAt":"2022-07-30T09:23:57.857Z","updatedAt":"2022-07-30T09:26:37.236Z","publishedAt":"2022-07-30T09:26:37.233Z"}}]}}},"meta":{}}
Here is how to request your collection types in Strapi using Next.js. When you create a collection type or a single type you will have a certain number of REST API endpoints available to interact with.
We just used the GET, POST and PUT methods here but you can get one entry, and delete an entry too. Learn more about API Endpoints.