Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Flask REST API Resourceful Blueprints Register API

I have no Idea what to do here it just says bummer! Try again (not a very helpful error message )

Great! Now that we have the Blueprint, it's time to create the actual Api instance and register the resources for it. Create a new variable named api in each of the resources that's an Api instance for the Blueprint. Then add both of the Resources to api. You can use whatever endpoint name you want, but make sure the URL pattern is like "/api/v1/[PLURAL RESOURCE]". Remember to include the id argument in the appropriate URLs.

api.py
from flask import Flask

app = Flask(__name__)


if __name__ == '__main__':
    app.run()
resources/ingredients.py
from flask import Blueprint
from flask.ext.restful import Resource, Api

import models


class IngredientList(Resource):
    def get(self):
        return 'IngredientList'


class Ingredient(Resource):
    def get(self, id):
        return 'Ingredient'

ingredients_api=Blueprint('resources.ingredients', __name__)

api = Api(ingredients_api)

api.add_resource(IngredientList, '/api/v1/ingredients', endpoint='ingredients')

api.add_resource(Ingredient, '/api/v1/ingredients/<int:id>', endpoint='ingredient')
resources/recipes.py
from flask import Blueprint
from flask.ext.restful import Resource, Api

import models


class RecipeList(Resource):
    def get(self):
        return 'RecipeList'


class Recipe(Resource):
    def get(self, id):
        return 'Recipe'

recipes_api=Blueprint('resources.recipes', __name__)

api = Api(recipes_api)

api.add_resources(RecipeList, '/api/v1/recipes', endpoint='recipes')

api.add_resources(Recipe, '/api/v1/recipes/<int:id>', endpoint='recipe')

2 Answers

Jay Meyer
Jay Meyer
7,708 Points

Hey Rodi,

You might have figured it out since this is 2 months old, but in case other people struggle here is my solution.

api.py

from flask import Flask

from resources.ingredients import ingredients_api
from resources.recipes import recipes_api

app = Flask(__name__)
app.register_blueprint(ingredients_api)
app.register_blueprint(recipes_api)

if __name__ == '__main__':
    app.run()

resources/ingredients

ingredients_api = Blueprint('resources.ingredients',__name__)
api = Api(ingredients_api)
api.add_resource(
    IngredientList,
    '/api/v1/ingredients',
    endpoint='ingredients'
)
api.add_resource(
    Ingredient,
    '/api/v1/ingredients/<int:id>',
    endpoint='ingredient'
)

resources/recipes

recipes_api = Blueprint('resources.recipes',__name__)   
api = Api(recipes_api)
api.add_resource(
    RecipeList,
    '/api/v1/recipes',
    endpoint='recipes'
)
api.add_resource(
    Recipe,
    '/api/v1/recipes/<int:id>',
    endpoint='recipe'
)
Patrick Rice
Patrick Rice
11,017 Points

In ingredients.py correctly space ingredients_api=Blueprint. In recipes.py change api.add_resources to api.add_resource (both times)