I'm playing with AWS Amplify since I've to introduce some of its feature in a legacy application that has no framework (no React, no Angular: just vanilla JS).
I used successfully the Auth module so I recreated a simple sign up/in/out strategy. What I want now is to call a REST API, using REST API module. So I created an API using API Gateway PetStore example and I would interact with it.
So I created the configuration:
import Amplify from '@aws-amplify/core';
Amplify.configure({
Auth: {
...
},
API: {
endpoints: [
{
name: "PetStore", // name of the API in API Gateway console
endpoint: "https://XXX.execute-api.eu-central-1.amazonaws.com/dev",
region: "eu-central-1",
paths: ['/']
}
]
}
});
and somewhere else:
import Api from '@aws-amplify/api-rest';
Api.get('PetStore', '/pets', {})
.then(response => {
console.log(response)
}).catch(error => {
console.log(error);
});
But, when executed, I get always the same error:
API PetStore does not exist
Any idea? Remember that:
The Amplify API category provides an interface for making requests to your backend. The Amplify CLI deploys REST APIs and handlers using Amazon API Gateway and AWS Lambda.
To do this you simply install the amplify library as normal. After that you need to manually configure any features you're going to use with Amplify. configure();. You can find the manual configuration in the Amplify documentation for each library you plan to use.
To call a deployed API, clients submit requests to the URL for the API Gateway component service for API execution, known as execute-api . The base URL for REST APIs is in the following format: https:// {restapi_id} .execute-api. {region} .amazonaws.com/ {stage_name} /.
Before you can invoke an API, you must deploy it in API Gateway. To do that, follow the instructions in Deploying a REST API in Amazon API Gateway . You can find a REST API's root URL in the Stage Editor for the API in the API Gateway console. It's listed as the Invoke URL at the top.
You can handle these in a script behind an HTML page or in a client application using one of the AWS SDKs. For testing, you can use the API Gateway console to call an API by using the API Gateway's TestInvoke feature, which bypasses the Invoke URL and allows API testing before the API is deployed.
Try changing the version to a previous version you know worked. API * does not exist usually means you haven't pushed the REST API you created. If you can't use amplify push have you manually created and amplify API through the console?
I'd like to add that @Deiv was correct to suggest API.configure(), this worked for me. After trying amplify pull
and amplify push
I still received the error API <api-name> does not exist
.
So my code now looks like this:
import Amplify, { API } from 'aws-amplify';
import awsconfig from './aws-exports';
Amplify.configure(awsconfig);
API.configure(awsconfig);
I am using npm package: aws-amplify 3.3.21
I had this same issue and I managed to resolve it by configuring the exports on the "Api" object directly:
API.configure(aws_exports);
Previously it worked for me with just setting it on Amplify.configure, but it seems a newer version of Amplify requires it on the sub module itself.
Some more bits and pieces can be found here in this long-standing issue (in which the same issue popped up for Auth, but I applied the same to both in my case and it fixed it): https://github.com/aws-amplify/amplify-js/issues/4315
You need to call API.configure()
after Amplify.configure
is called, or else your API setup won't be applied, hence it returns API PetStore does not exist
import Amplify, { API } from 'aws-amplify'; // this part depends on which Amplify you are using. but make sure to import API
Amplify.configure({
Auth: {
...
},
API: {
endpoints: [
{
name: "PetStore", // name of the API in API Gateway console
endpoint: "https://XXX.execute-api.eu-central-1.amazonaws.com/dev",
region: "eu-central-1",
paths: ['/']
}
]
}
});
// Call this
API.configure(); // no awsconfig required as you have set your own
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With