This is example of APIs written in Laravel 8 and using Laravel Passport for OAuth2 authentication and authorization. This project uses an externa API to get stock quotes and the built-in Laravel notification system. There is also a Postman file to see and test all endpoints.
Here are the steps to get this project up and running.
- Clone the repository
- Run
composer install cdinto the directory the repo was cloned intocp .env.example .envto create environment file.- Edit
.envfile adding you database connection info. Make sure the database has been created and is empty. - Add your API key obtained from Alphavantage to the '.env' file under STOCK_API_KEY
- Migrate the database by running
php artisan migrate - Generate encryption keys for OAuth2 by running
php artisan passport:keys - Generate application key by running
php artisan key:generate - Create client credentials in passport
php artisan passport:client --passwordUse these to obtain a token to use the api.
The application is now installed and ready to go!
- In the root of the project run
php artisan serveto run the server - Load the postman file into postman and use the API to create a user.
- Login using the client ID and client secret from step 9 above and the user's email and password. Once you have a token a token you can use it for subsequent requests. Under authentication in the Postman collection you can obtain and save the token.
Enjoy using the API!
POST api/users
Parameters
name The user's full name
email The user's email
password The user's password
password_confirmation Confirmation of the password
POST oauth/token
Parameters
grant_type = 'password' User 'password' as the value for this field
client_id Client id received in step 9 of installation
client_secret Client secret received in step 9 of installation
username User's email address
password User's password
scope Leave this value blank
This will return a token for you to use to as all endpoints
GET users
Will get info for logged in user.
GET api/stock/:symbol/:price
Path Parameters
symbol The symbol of the stock to lookup
price The price you bought the stock at
This will get the current price and volume for the stock and create a notification telling you how much you've gained or lost.
GET api/notifications
Get all notifications for the logged in user.
POST api/notifications
Parameters
message The message for the notification
PUT api/notifications/:id
Path Parameters
id The id of the notification to update
Parameters
message The new message to replace the old one
DELETE api/notifications/:id
Path Parameters
id The id of the notification to delete
PUT api/notifications/:id/read
Path Parameters
id The id of the notification to mark as read
PUT api/notifications/:id/unread
Path Parameters
id The id of the notification to mark as unread