英文:
I'm building a create-next-app and i have to call data from a web api, but i don't know where to store the API key
问题
I'm trying to create a create-next-app which fetch data from https://newscatcherapi.com/free-news-api and displaying it on my app.
我正在尝试创建一个create-next-app,用它从https://newscatcherapi.com/free-news-api获取数据并在我的应用程序上显示它。
I got API key to access the API. But i don't where to save the API key and make call to fetch the API with API key.
我获得了用于访问API的API密钥,但我不知道在哪里保存API密钥并使用API密钥进行API调用。
Expecting any links to resources or any other answers to learn API call with api key.
期望提供有关学习如何使用API密钥进行API调用的任何资源链接或其他答案。
英文:
I'm trying to create a create-next-app which fetch data from https://newscatcherapi.com/free-news-api and displaying it on my app.
I got API key to access the API. But i don't where to save the API key and make call to fetch the API with API key.
Expecting any links to resources or any other answers to learn API call with api key.
答案1
得分: 1
API密钥是您不想分享的东西,所以您要做的是将它们保存在您的服务器上,这样就没有人能看到它们。这也意味着您需要在服务器上使用您的API密钥获取数据,然后发送最终数据,您可以通过在app目录中使用API路由或者现在通过使用服务器操作来实现,但我个人不建议使用此功能,因为它目前处于α测试阶段。这是一种实现API的方式。
回到存储API密钥的位置,我们倾向于使用称为环境变量的东西来存储它们,您可以在这里阅读更多关于它们的信息,如果没有指定,它们不会暴露给浏览器。
英文:
API keys are something you don't want to share, so what you want to do is to keep them on your server so no one will be able to see them this also means that you need to fetch the data using your api key on the server and then send the final data, you can do it by using api route in app dir or now by using server action which I would personally not recommend due to this feature being currently in alpha so here is a way to implement an api.
https://youtu.be/vrR4MlB7nBI
Back to where to store your api keys we prefer to store them using something called Environment Variables which you can read more about here they will not be exposed to the browser if not specified.
https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论