Blogospot (Blog API)
Tags: javascript, node, api, tutorial
Published: 2023-01-10 06:14:36

Blogospot is a **RESTful** blog API. Users of the Blogospot RESTful API can create, modify, publish, and remove blogs as well as access a list of all blogs that have already been published. JWT (Jason Web Token) is used by the API for authentication and authorization to give consumers privacy and security. We'll cover a brief overview of how to get started with the blog API in this article

---


## prerequisites 

- Javascript
- Node js
- MongoDb

----

## Requirements
###Users must have the following fields:

- first_name
- second_name
- email
- password


###A logged in user will be able to:

- Sign up and sign in to the blog app
- View a list of published blogs
- View a single published blog
- Create a blog (in draft state)
- Update the state of a blog to published (if the user is the owner of the blog)
- Edit a blog in draft or published state (if the user is the owner of the blog)
- Delete a blog in draft or published state (if the user is the owner of the blog)
- View a list of their own blogs

###A blog may contain the following fields:

- title
- description
- tags
- author (user)
- timestamp
- state (either "draft" or "published")
- read_count
- reading_time
- body


The list of published blogs that can be accessed by both logged in and not logged in users must be:

- Paginated (default of 20 blogs per page)
- Searchable by author, title, and tags
- Orderable reading_time, and timestamp


## Development

## Base URL

The base URL for the api is `https://misty-cowboy-hat-crow.cyclic.app/`


## Models

---

### User

| field      | data_type | constraints      |
| ---------- | --------- | ---------------- |
| id         | string    | required         |
| username   | string    | required, unique |
| firs_tname | string    | required         |
| last_name  | string    | required         |
| email      | string    | required, unique |
| password   | string    | required         |

### Blog

| field        | data_type  | constraints                                              |
| ------------ | ---------- | -------------------------------------------------------- |
| title        | string     | required, unique                                         |
| description  | string     | optional                                                 |
| author       | ref - User |                                                          |                                              |
| state        | string     | required, default: 'draft', enum: ['draft', 'published'] |
| read_count   | Number     | default: 0                                               |
| reading_time | Number     |                                                          |
| tags         | array      | optional                                                 |
| body         | string     | required                                                 |

---

##APIs

###Creating a user

- Route : `/api/v1/signup`

- Mthod : `POST`

:point_down: Body
```json
{
  "first_name": "firstName",
  "last_name": "secondName",
  "username": "username",
  "email": "email@gmail.com",
  "password": "1234567"
}
```
:point_down: Response
```json
{
    "status": true,
    "data": {
        "first_name": "firstName",
        "last_name": "secondName",
        "username": "username",
        "email": "email@gmail.com",
        "_id": "63bd099a16b1654c5a842c8c",
        "__v": 0
    
}
```


###Login a user

- Route : `/api/v1/login`

- Mthod : `POST`

:point_down: Body
```json
{
  "email": "email@gmail.com",
  "password": "1234567"
}
```
:point_down: Response
```json
{
    "status": true,
    "data": {
        "first_name": "firstName",
        "last_name": "secondName",
        "username": "username",
        "email": "email@gmail.com",
        "_id": "63bd099a16b1654c5a842c8c",
}
```

### Create Blog

- Route: `api/v1/blogospot/blogs`
- Method: POST
- Header
  - Authorization: cookie {token}

:point_down: Body

```json
{
  "title": "List",
  "description": "PoliticsVerse  @PoliticsVerse · 6h LiCompanies who hav",

  "tags": ["Airlines", "America"],
  "state": "published",
  "body": "Skip to comments.\r\nList twiter ^Posted on 11/05/2022 1:45:38 PM PDT by janetjanet998\r\nPoliticsVerse @PoliticsVerse · 6h List of Companies who have suspended Twitter Ads: \r\nGeneral Mills CVS United Ai… [+1616 chars]"
}
```

:point_down: Response

```json
{
  "status": "true",
  "data": {
    "title": "List",
    "description": "PoliticsVerse  @PoliticsVerse · 6h LiCompanies who hav",
    "author": "6367c296ba7522bd8561e4f6",
    "state": "draft",
    "read_count": 0,
    "tags": ["Airlines", "America"],
    "body": "Skip to comments.\r\nList twiter ^Posted on 11/05/ 2022 1:45:38 PM PDT by janetjanet998\r\nPoliticsVerse @PoliticsVerse · 6h List of Companies who have suspended Twitter Ads: \r\nGeneral Mills CVS United Ai… [+1616 chars]",
    "_id": "6367cc2271c384885108032f",
    "createdAt": "2022-11-06T15:00:50.202Z",
    "updatedAt": "2022-11-06T15:00:50.202Z",
    "reading_time": 2
  }
}
```


###Get all Blogs

- Route: `/api/v1/login/blogospot/blogs`
- Method: get

:point_down: Body

- Get all published blogs
- filter by tags  
- filter by title
- sort by timestamp
- sort by reading_time, timestamp

:point_down: Response
```json
{
  "status": "true",
  "data": {
    "_id": "6367cc2271c384885108032f",
    "title": "The Adventures of John",
    "description": "Fun times as Johnny",
    "author": "6367c296ba7522bd8561e4f6",
    "state": "published",
    "read_count": 0,
    "tags": ["memoirs", "expose"],
    "body": "A very fun article that is long enough to be fun, and short enough to be ..fun! A sailor went to sea to see what he could see but all that he could see was the bottom of the deep blue sea.",
    "createdAt": "2022-11-06T15:00:50.202Z",
    "updatedAt": "2022-11-06T16:22:29.326Z",
    "reading_time": 1
  }
}
```

###Blogs by a user

- Route: `/api/v1/login/blogospot/blogs/user`

- Method: get

####All blogs created by a specific user

:point_down: Response
```json
{
  "status": "true",
  "data": {
    "_id": "6367cc2271c384885108032f",
    "title": "The Adventures of John",
    "description": "Fun times as Johnny",
    "author": "6367c296ba7522bd8561e4f6",
    "state": "published",
    "read_count": 0,
    "tags": ["memoirs", "expose"],
    "body": "A very fun article that is long enough to be fun, and short enough to be ..fun! A sailor went to sea to see what he could see but all that he could see was the bottom of the deep blue sea.",
    "createdAt": "2022-11-06T15:00:50.202Z",
    "updatedAt": "2022-11-06T16:22:29.326Z",
    "reading_time": 1
  },
{
    "_id": "6367cc2271c384885108032f",
    "title": "The Adventures of John",
    "description": "Fun times as Johnny",
    "author": "6367c296ba7522bd8561e4f6",
    "state": "published",
    "read_count": 0,
    "tags": ["memoirs", "expose"],
    "body": "A very fun article that is long enough to be fun, and short enough to be ..fun! A sailor went to sea to see what he could see but all that he could see was the bottom of the deep blue sea.",
    "createdAt": "2022-11-06T15:00:50.202Z",
    "updatedAt": "2022-11-06T16:22:29.326Z",
    "reading_time": 1
  }
}
```

###Update the content of a blog

- Route: `api/v1/blogospot/blogs/:id`
- Method: PATCH
- Header
- Authorization: cookie{token}
- Requirement: User must be logged

:point_down: Body

```json
{
  "state": "published",
  "tags": ["memoirs", "expose"],
  "body": "A very fun article that is long enough to be fun, and short enough to be ..fun! A sailor went to sea to see what he could see but all that he could see was the bottom of the deep blue sea."
}
```

:point_down: Response

```json
{
  "status": "true",
  "data": {
    "_id": "6367cc2271c384885108032f",
    "title": "List",
    "description": "PoliticsVerse  @PoliticsVerse · 6h LiCompanies who hav",
    "author": "6367c296ba7522bd8561e4f6",
    "state": "published",
    "read_count": 0,
    "tags": ["memoirs", "expose"],
    "body": "A very fun article that is long enough to be fun, and short enough to be ..fun! A sailor went to sea to see what he could see but all that he could see was the bottom of the deep blue sea.",
    "createdAt": "2022-11-06T15:00:50.202Z",
    "updatedAt": "2022-11-06T16:22:29.326Z",
    "reading_time": 1
  }
}
```


###Delete Blog

- Route: `api/v1/blogospot/blogs/:id`
- Method: DELEtE
- Requirement: User must be llogged in

- Delete the blog with the specified id, and returns the deleted blog

```json
{
        status: true,
        message: "Deleted successfully"
}
```



###Error handling
- All error will be returned in this json format

```json
{
  status: false, 
  error: "Error message"
}
```

## Conclusion
I hope this brief article on blogospot API helps you navigate and implement the API for your app easily.