{"id":20237447,"url":"https://github.com/nchimunyascripts/simple_flask_product_api","last_synced_at":"2026-06-05T03:32:29.190Z","repository":{"id":253480725,"uuid":"840916894","full_name":"nchimunyascripts/simple_flask_product_API","owner":"nchimunyascripts","description":null,"archived":false,"fork":false,"pushed_at":"2024-08-17T01:45:10.000Z","size":12412,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-14T01:11:00.466Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nchimunyascripts.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-11T04:50:29.000Z","updated_at":"2024-08-17T01:45:13.000Z","dependencies_parsed_at":"2024-08-17T02:54:42.527Z","dependency_job_id":null,"html_url":"https://github.com/nchimunyascripts/simple_flask_product_API","commit_stats":null,"previous_names":["nchimunyascripts/simple_flask_product_api"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nchimunyascripts%2Fsimple_flask_product_API","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nchimunyascripts%2Fsimple_flask_product_API/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nchimunyascripts%2Fsimple_flask_product_API/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nchimunyascripts%2Fsimple_flask_product_API/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nchimunyascripts","download_url":"https://codeload.github.com/nchimunyascripts/simple_flask_product_API/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241681854,"owners_count":20002391,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-14T08:26:59.994Z","updated_at":"2026-06-05T03:32:29.167Z","avatar_url":"https://github.com/nchimunyascripts.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Flask RESTful API with SQLAlchemy\n\nThis project demonstrates how to build a RESTful API using Flask and SQLAlchemy. The API supports basic CRUD (Create, Read, Update, Delete) operations for managing items.\n\n## Prerequisites\n\n- Python 3.x\n- pip (Python package installer)\n\n## Project Setup\n\n### Step 1: Clone the Repository\n\nClone this repository to your local machine:\n\n```bash\ngit clone https://github.com/your-username/flask-restful-api.git\ncd flask-restful-api\n```\n\n### Step 2: Set Up the Virtual Environment\n\nCreate and activate a virtual environment:\n\n```bash\n# Create virtual environment\npython -m venv venv\n\n# Activate virtual environment\nsource venv/bin/activate  # On Windows use `venv\\Scripts\\activate`\n```\n\n### Step 3: Install Dependencies\n\n#### Option 1: Install Manually\n\nInstall the necessary Python packages manually:\n\n```bash\npip install Flask Flask-SQLAlchemy\n```\n\n#### Option 2: Install from `requirements.txt`\n\nAlternatively, you can install all dependencies from the `requirements.txt` file:\n\n```bash\npip install -r requirements.txt\n```\n\nThe `requirements.txt` file should look like this:\n\n```\nFlask==2.1.1\nFlask-SQLAlchemy==2.5.1\n```\n\n### Step 4: Project Structure\n\nThe project is structured as follows:\n\n```\nflask_api/\n│\n├── app.py           # Main application file\n├── models.py        # Database models\n├── config.py        # Configuration settings\n└── requirements.txt # List of dependencies\n```\n\n### Step 5: Configuration\n\nThe `config.py` file contains the configuration settings for the project, including the secret key and database URI.\n\n```python\nimport os\n\nbasedir = os.path.abspath(os.path.dirname(__file__))\n\nclass Config:\n    SECRET_KEY = os.environ.get('SECRET_KEY') or 'a_secret_key'\n    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \\\n                              'sqlite:///' + os.path.join(basedir, 'app.db')\n    SQLALCHEMY_TRACK_MODIFICATIONS = False\n```\n\n### Step 6: Define the Models\n\nThe `models.py` file defines the database models used in the project:\n\n```python\nfrom flask_sqlalchemy import SQLAlchemy\n\ndb = SQLAlchemy()\n\nclass Item(db.Model):\n    id = db.Column(db.Integer, primary_key=True)\n    name = db.Column(db.String(50), nullable=False)\n    description = db.Column(db.String(200), nullable=True)\n\n    def to_dict(self):\n        return {\n            'id': self.id,\n            'name': self.name,\n            'description': self.description\n        }\n```\n\n### Step 7: Application Setup\n\nThe main application logic is in `app.py`, where the routes and CRUD operations are defined:\n\n```python\nfrom flask import Flask, request, jsonify, abort\nfrom config import Config\nfrom models import db, Item\n\napp = Flask(__name__)\napp.config.from_object(Config)\ndb.init_app(app)\n\n@app.route('/items', methods=['GET'])\ndef get_items():\n    items = Item.query.all()\n    return jsonify([item.to_dict() for item in items]), 200\n\n@app.route('/items/\u003cint:item_id\u003e', methods=['GET'])\ndef get_item(item_id):\n    item = Item.query.get_or_404(item_id)\n    return jsonify(item.to_dict()), 200\n\n@app.route('/items', methods=['POST'])\ndef create_item():\n    data = request.get_json()\n    if not data or not 'name' in data:\n        abort(400, description=\"Name is required\")\n    item = Item(name=data['name'], description=data.get('description'))\n    db.session.add(item)\n    db.session.commit()\n    return jsonify(item.to_dict()), 201\n\n@app.route('/items/\u003cint:item_id\u003e', methods=['PUT'])\ndef update_item(item_id):\n    item = Item.query.get_or_404(item_id)\n    data = request.get_json()\n    if not data or not 'name' in data:\n        abort(400, description=\"Name is required\")\n    item.name = data['name']\n    item.description = data.get('description')\n    db.session.commit()\n    return jsonify(item.to_dict()), 200\n\n@app.route('/items/\u003cint:item_id\u003e', methods=['DELETE'])\ndef delete_item(item_id):\n    item = Item.query.get_or_404(item_id)\n    db.session.delete(item)\n    db.session.commit()\n    return '', 204\n\nif __name__ == '__main__':\n    with app.app_context():\n        db.create_all()\n    app.run(debug=True)\n```\n\n### Step 8: Run the Application\n\nTo start the Flask application, run:\n\n```bash\npython app.py\n```\n\n### API Endpoints\n\n- **GET /items**: Retrieve all items\n- **GET /items/\u003citem_id\u003e**: Retrieve a single item by ID\n- **POST /items**: Create a new item\n- **PUT /items/\u003citem_id\u003e**: Update an existing item by ID\n- **DELETE /items/\u003citem_id\u003e**: Delete an item by ID\n\n### Testing the API\n\nYou can use tools like Postman or curl to interact with the API and perform CRUD operations.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n---\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnchimunyascripts%2Fsimple_flask_product_api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnchimunyascripts%2Fsimple_flask_product_api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnchimunyascripts%2Fsimple_flask_product_api/lists"}