System APIs of Pastebin
When designing a Pastebin system, APIs (Application Programming Interfaces) play a central role in enabling interactions between users, the front-end, and the back-end services. These APIs will facilitate operations like creating pastes, retrieving pastes, deleting them, and managing user preferences. Below is an outline of the essential System APIs for a Pastebin application that you can explain to your students.
1. API for Creating a Paste
Endpoint: POST /api/paste
- Description: This API endpoint allows users to create a new paste by submitting content (text or code) to the system. The system will store the content and generate a unique URL for the paste.
Request Parameters:
- content (string): The text or code to be pasted.
- title (optional, string): The title for the paste (e.g., “Sample Python Code”).
- language (optional, string): The programming language for syntax highlighting (e.g., “python”, “javascript”).
- visibility (optional, string): Defines if the paste is public or private.
- expire_in (optional, string): Time to live for the paste (e.g., “7 days”, “1 hour”).
Response:
- paste_url (string): A URL that can be shared with others to access the paste.
- status (string): The status of the operation (e.g., “success”).
- paste_id (string): The unique identifier for the paste.
Example Request:
Example Response:
2. API for Retrieving a Paste
Endpoint: GET /api/paste/{paste_id}
- Description: This API endpoint allows users to retrieve a specific paste using its unique
paste_id
. It will return the content of the paste along with metadata such as language and creation time.
Request Parameters:
- paste_id (path parameter): The unique identifier of the paste to retrieve.
Response:
- content (string): The text or code content stored in the paste.
- language (string): The programming language associated with the paste (e.g., “python”).
- title (string, optional): The title of the paste.
- created_at (string): Timestamp of when the paste was created.
- expires_at (string): Timestamp of when the paste expires.
Example Request:
Example Response:
3. API for Deleting a Paste
Endpoint: DELETE /api/paste/{paste_id}
- Description: This API allows users to delete a specific paste by its
paste_id
. It removes both the content and metadata associated with the paste from the system.
Request Parameters:
-
paste_id (path parameter): The unique identifier of the paste to delete.
Response:
- status (string): The status of the operation (e.g., “success”, “not found”).
Example Request:
Example Response:
4. API for Listing User Pastes
Endpoint: GET /api/user/pastes
- Description: This API endpoint retrieves a list of all pastes created by the authenticated user. The user can fetch metadata like titles and creation timestamps of their pastes.
Request Parameters:
- user_id (query parameter): The unique identifier of the user (if not authenticated through a session).
- limit (optional, integer): The number of pastes to retrieve in one request.
- offset (optional, integer): The page number or offset to start fetching from.
Response:
- pastes (array): An array of paste objects, each containing metadata.
- paste_id (string): Unique identifier of the paste.
- title (string): The title of the paste.
- created_at (string): Timestamp when the paste was created.
Example Request:
Example Response:
5. API for Managing Paste Expiration
Endpoint: POST /api/paste/expire/{paste_id}
- Description: This API allows users to set or modify the expiration time of an existing paste. The paste will be deleted once it expires.
Request Parameters:
- paste_id (path parameter): The unique identifier of the paste to modify.
- expire_in (string): Time duration after which the paste will expire (e.g., “1 hour”, “30 days”).
Response:
- status (string): The status of the operation (e.g., “success”).
- expire_at (string): The new expiration timestamp.
Example Request:
Example Response:
6. API for Searching Pastes
Endpoint: GET /api/search
- Description: This API allows users to search for pastes based on a query string. It is useful for finding pastes with certain keywords, titles, or content.
Request Parameters:
- query (query parameter): The search term to look for in the paste content or title.
- limit (optional, integer): The number of results to return.
- offset (optional, integer): The page number or offset to start fetching from.
Response:
- results (array): A list of pastes that match the search query.
- paste_id (string): Unique identifier of the paste.
- title (string): Title of the paste.
- created_at (string): Timestamp when the paste was created.
Example Request:
Example Response:
7. API for User Authentication (Optional)
Endpoint: POST /api/auth/login
- Description: This API allows users to authenticate with their username and password, returning a token for session management.
Request Parameters:
- username (string): The user’s username.
- password (string): The user’s password.
Response:
- token (string): A session token to authenticate further requests.
- status (string): The status of the operation (e.g., “success”).
Example Request:
Example Response:
Conclusion
These System APIs for a Pastebin system allow users to interact with the application in a structured and predictable way. These APIs provide basic operations such as creating, retrieving, updating, and deleting pastes, along with the ability to manage user-specific data, expiration times, and privacy settings. By explaining these APIs, students can better understand the core functions of a web service and how RESTful APIs work in a real-world application.