Lesson Details

We want our API to be accessible on the /api/pokemon route, so in netlify.toml we can add a rewrite, which is different than a redirect.

A redirect will change the url in the browser, while a rewrite will not. So the user will still see /api/pokemon/:slug instead of seeing the URL change to /.netlify/functions/pokemon. We specifically need to use the 200 status code to get a rewrite.

We can put the rewrite in the netlify.toml under the redirects key. Netlify allows us to specify a from url, a to url, the status code, and more if we wanted

[[redirects]]
from = "/api/pokemon/:slug"
to = "/.netlify/functions/pokemon-api"
status = 200

Now redeploy:

DATABASE_URL=mysql://127.0.0.1:3306 cargo lambda build -p pokemon-api
cp target/lambda/pokemon-api/bootstrap functions/pokemon-api
netlify deploy --prod

and we can use our new URL /api/pokemon/:slug.

curl https://<site-name>.netlify.app/api/pokemon/charizard

To get JSON responses including the pokemon

{"name":"Charizard","hp":78}