#14957·amplify-js

resolveApiUrl mislabels any thrown error (e.g. missing URL.canParse) as "API name is invalid"

Author: dmagil-edfCreated Sep 22, 2026Updated Sep 23, 2026
LabelsbugAPI

Describe the bug

resolveApiUrl in @aws-amplify/api-rest calls AmplifyUrl.canParse(urlStr + path) (where AmplifyUrl is a plain alias for the global URL) with no feature detection, and wraps the whole resolution logic in a try/catch that relabels any thrown error as RestApiValidationErrorCode.InvalidApiName ("API name is invalid").

Source (unchanged as of the latest 4.7.0 / @aws-amplify/[email protected]):

javascript
const resolveApiUrl = (amplify, apiName, path, queryParams) => {
    const urlStr = amplify.resourcesConfig?.API?.REST?.[apiName]?.endpoint;
    assertValidationError(!!urlStr, RestApiValidationErrorCode.InvalidApiName);
    try {
        let url;
        if (AmplifyUrl.canParse(urlStr + path)) {
            url = new AmplifyUrl(urlStr + path);
        } else {
            url = new AmplifyUrl(urlStr + path, location?.origin);
        }
        ...
        return url;
    }
    catch (error) {
        throw new RestApiError({
            name: RestApiValidationErrorCode.InvalidApiName,
            ...validationErrorMap[RestApiValidationErrorCode.InvalidApiName],
            recoverySuggestion: `Please make sure the REST endpoint URL is a valid URL string. Got ${urlStr}`,
        });
    }
};

URL.canParse is a relatively recent Web API (Chrome/Edge 120+, Dec 2023; Safari/WebKit 17+, Sept 2023; Firefox 115+). On any browser engine that predates this — crucially all iOS/iPadOS browsers below iOS 17 (Apple requires every third-party browser, including Chrome/Firefox/Edge on iOS, to use the system WebKit engine, so the reported browser name/version is irrelevant — only the OS version matters), and older desktop Chromium/Firefox/Safari — calling AmplifyUrl.canParse(...) throws a TypeError: url.canParse is not a function.

That TypeError is caught by the surrounding try/catch and re-thrown as InvalidApiName — a completely misleading error that has nothing to do with the actual apiName being valid or invalid. It happens even though:

  • The API name given by the caller is correct.
  • The configured endpoint URL is well-formed.
  • The request would otherwise succeed on a browser with URL.canParse support.

Reproduction

  1. In any environment lacking URL.canParse (e.g. Chrome DevTools console: delete URL.canParse, or genuinely test on iOS 16 Safari/Chrome), call any Amplify REST get/post/etc. with a correctly configured apiName.
  2. Observe the thrown error is RestApiError with message "API name is invalid", even though the endpoint config and API name are both correct.

Expected behavior

Either:

  1. Feature-detect AmplifyUrl.canParse before calling it (e.g. typeof AmplifyUrl.canParse === 'function' ? AmplifyUrl.canParse(...) : /* fallback via try/catch new URL(...) */), so the library works correctly on older/WebKit-locked engines without requiring consumers to polyfill a core browser API themselves; and/or
  2. At minimum, don't swallow unrelated exceptions (like a TypeError from a missing method) into InvalidApiName — preserve/surface the original error so it's actually debuggable.

Impact

We saw this manifest in production as intermittent "API name is invalid" errors, confirmed via RUM/session data to correlate almost exactly with usersession.osVersion < 17 on iOS/iPadOS (independent of the browser's self-reported name, since Apple forces all iOS browsers onto WebKit). This was very difficult to diagnose because the error message actively points away from the real cause. We worked around it in our own app with a URL.canParse polyfill installed before Amplify's config runs, but this really belongs in the library since it's calling a non-universally-supported Web API without a safety net.

Amplify (JS) Version

@aws-amplify/[email protected], @aws-amplify/[email protected] (confirmed still present, unchanged, in latest 4.7.0/6.17.0 as of 2026-09-22)

Backend

REST (API Gateway)

Additional information

Happy to submit a PR for the feature-detection fix if that's welcome — the change would be small and localized to resolveApiUrl.ts.