Tiny Rest API for teaching purposes. Express, Typescript and Prisma. Try this service on this Live Demo
Configure your environment variables on .env
file:
DATABASE_URL="mysql://root:root@localhost/test"
JWT_SECRET="secret"
Install dependencies with:
npm install
Push DB changes with:
npx prisma db push
The app counts with the following endpoints:
Returns information about the app.
Returns 200 code if the app is running.
Returns a JWT token if the credentials are correct.
Example request:
Method: POST
Endpoint: /api/v1/auth/login
Body:
{
"email": "[email protected]",
"password": "password"
}
Example succesfully response:
Status: 200
Body:
{
"token": "<jwt_token>"
}
Returns the new user if email doesnt exists.
Example request:
Method: POST
Endpoint: /api/v1/auth/register
Body:
{
"email": "[email protected]",
"password": "password"
}
Example succesfully response:
Status: 201
Body:
{
"id": 1,
"email": "[email protected]"
}
Returns all user's pets. Requires Bearer Token at Authorization Header
Example request:
Method: GET
Endpoint: /api/v1/pets
Example succesfully response:
Status: 200
Body:
[
{
"id": 1,
"name": "Cheems",
"type": "Dog",
"birth": "2020-01-01T00:00:00.000Z",
"photo": "someUrl",
"userId": 1
}
]
Return a pet by id. Requires Bearer Token at Authorization Header
Example request:
Method: GET
Endpoint: /api/v1/pets/1
Example succesfully response:
Status: 200
Body:
{
"id": 1,
"name": "Cheems",
"type": "Dog",
"birth": "2020-01-01T00:00:00.000Z",
"photo": "someUrl",
"userId": 1
}
Create a new pet for logged user. Requires Bearer Token at Authorization Header
Example request:
Method: POST
Endpoint: /api/v1/pets
Body:
{
"name": "Cheems",
"type": "Dog",
"birth": "2020-01-01T00:00:00.000Z",
"photo": "someUrl"
}
Example successfully response:
Status: 201
Body:
{
"id": 1,
"name": "Cheems",
"type": "Dog",
"birth": "2020-01-01T00:00:00.000Z",
"photo": "someUrl",
"userId": 1
}
Update a pet by id. Requires Bearer Token at Authorization Header
Example request:
Method: PUT
Endpoint: /api/v1/pets/1
Body:
{
"name": "NotCheems",
"type": "Cat"
}
Example successfully response:
Status: 204
Delete a pet by id. Requires Bearer Token at Authorization Header
Example request:
Method: DELETE
Endpoint: /api/v1/pets/1
Example successfully response:
Status: 204