Rest API repository for an IoT project, built in C# using the .NET 6.0 framework, with a MySQL database and JWT authentication.
This repository contains a Rest API for an IoT project that will be consumed by a Kotlin app and also used with Arduino. The characteristics and functionalities of the API are detailed below.
- The API is built in C# using the .NET 6.0 framework.
- A MySQL database is used to store sensor data.
- JWT authentication is used to secure endpoints that require authentication.
- Swagger is used to document the API.
The API has the following endpoints:
/
: Test endpoint that returns a welcome message./login
: Endpoint to authenticate users and obtain a JWT token./sensors
: Endpoint to retrieve the list of sensors stored in the database. Requires authentication./sensors/{id}
: Endpoint to retrieve, update, or delete a specific sensor. Requires authentication.
To use the API, follow these steps:
- Clone the repository to the local machine.
- Configure the database connection in the appsettings.json file.
- Compile and run the API.
- Use an HTTP client, such as Postman, to make requests to the API.
Below is an example of how to use the API to retrieve the list of sensors:
- To access the API, you must send an HTTP request to the corresponding endpoint, for example:
GET https://localhost:7129/
To change the port, you must modify the launchSettings.json file.
"profiles": {
"SmarthomeRest": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7129;http://localhost:5008",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
- To authenticate, you should send a POST request to the /login endpoint with the following body in JSON format:
{
"username": "admin",
"password": "admin"
}
- The response will contain a JWT token that should be sent in the Authorization header of subsequent requests, like this:
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE2OTQxMDc3OTksImV4cCI6MTY5NDExMTM5OSwiaWF0IjoxNjk0MTA3Nzk5fQ.GlJPznxoJdBskGIG8IVkLzk9m2b8ht3886I5IRL1GdU"
-
Send a GET request to the /sensors endpoint with the JWT token in the
Authorization
header. -
The API will return a JSON with the list of sensors stored in the database.
[
{
"id": 1,
"name": "Sensor de temperatura",
"value": 25.5,
"date": "2023-09-07T12:00:00"
},
{
"id": 2,
"name": "Sensor de humedad",
"value": 60.0,
"date": "2023-09-07T12:01:00"
}
]
For more information about available endpoints and the parameters they accept, you can consult the documentation generated with Swagger at the path "localhost:PORT/swagger"
Swagger
{
"openapi": "3.0.1",
"info": {
"title": "Mi SmartHome API",
"version": "v1"
},
"paths": {
"/": {
"get": {
"tags": [
"SmarthomeRest"
],
"responses": {
"200": {
"description": "Success",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
}
}
}
},
"/login": {
"post": {
"tags": [
"SmarthomeRest"
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/User"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Success"
}
}
}
},
"/sensores": {
"get": {
"tags": [
"SmarthomeRest"
],
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Sensor"
}
}
}
}
}
}
},
"post": {
"tags": [
"SmarthomeRest"
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Sensor"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Success"
}
}
}
},
"/sensores/{id}": {
"get": {
"tags": [
"SmarthomeRest"
],
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"schema": {
"type": "integer",
"format": "int32"
}
}
],
"responses": {
"200": {
"description": "Success"
}
}
},
"put": {
"tags": [
"SmarthomeRest"
],
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"schema": {
"type": "integer",
"format": "int32"
}
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Sensor"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Success"
}
}
},
"delete": {
"tags": [
"SmarthomeRest"
],
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"schema": {
"type": "integer",
"format": "int32"
}
}
],
"responses": {
"200": {
"description": "Success"
}
}
}
}
},
"components": {
"schemas": {
"Sensor": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int32"
},
"name": {
"type": "string",
"nullable": true
},
"value": {
"type": "number",
"format": "double"
},
"date": {
"type": "string",
"format": "date-time"
}
},
"additionalProperties": false
},
"User": {
"type": "object",
"properties": {
"username": {
"type": "string",
"nullable": true
},
"password": {
"type": "string",
"nullable": true
}
},
"additionalProperties": false
}
},
"securitySchemes": {
"Bearer": {
"type": "apiKey",
"description": "JSON Web Token based security",
"name": "Authorization",
"in": "header"
}
}
},
"security": [
{
"Bearer": [ ]
}
]
}