BAD_SEED
BAD_SEED

Reputation: 5056

AWS Amplify REST API cannot find the API

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.

enter image description here

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:

Upvotes: 9

Views: 9443

Answers (6)

NeG
NeG

Reputation: 1

I might be replying on an old thread but I am also facing the similar issue with aws amplify REST API. So for the error API does not exist, I put the below Amplify.configure in index.js file of the JavaScript project and it worked.

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: ['/']
            }
        ]
    }
});

Hope this will help someone.

Upvotes: 0

tomoima525
tomoima525

Reputation: 846

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

Upvotes: 2

Isaac I9
Isaac I9

Reputation: 169

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

Upvotes: 8

ruanpotgieter95
ruanpotgieter95

Reputation: 46

We changed the version of aws-amplify from 3 back to a previous version "aws-amplify": "^2.2.2" in package.json and that resolved it.

Think version 3 broke the manual configuration of aws-amplify.

Try changing the version to a previous version you know worked.

Upvotes: 0

Deiv
Deiv

Reputation: 3097

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

Upvotes: 3

pnewhook
pnewhook

Reputation: 4078

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?

Upvotes: 0

Related Questions