strapi/docs/start/api.md

56 lines
1.3 KiB
Markdown
Raw Normal View History

2016-03-22 18:11:11 +01:00
---
title: Your first API
---
## Create your first API
It's pretty simple to generate an API with the Strapi CLI:
```bash
2016-03-25 22:22:34 +01:00
$ strapi generate:api apiName
2016-03-22 18:11:11 +01:00
```
2016-03-25 22:22:34 +01:00
For example, you can create a `car` API with:
2016-03-22 18:11:11 +01:00
```bash
2016-03-25 22:22:34 +01:00
$ strapi generate:api car
2016-03-22 18:11:11 +01:00
```
## Update your database
Migrations allow you to define sets of schema changes so upgrading a database is a breeze.
### Create a new migration file
To generate a migration file run:
```bash
2016-03-25 22:22:34 +01:00
$ strapi migrate:make connection_name migration_name
2016-03-22 18:11:11 +01:00
```
For example, if you want to create a migration file named `new_car_model` for the `car` API we just generated using the `default` connection the command looks like:
```bash
$ strapi migrate:make default new_car_model
```
Be careful, migrations are automatically generated based on your current database schema and models. We strongly advise you to manually verify those information.
### Run the migrations
Once you have finished writing the migrations, you can update the database by running:
```bash
2016-03-25 22:22:34 +01:00
$ strapi migrate:run connection_name
2016-03-22 18:11:11 +01:00
```
So if you want to run the previous migration generated for the `default` connection you need to run:
```bash
$ strapi migrate:run default
```
## Consume your API
You can take a look at the routes of the generated `car` API at `./api/car/config/routes.json`.