Error: this environment is missing the following Web Fetch API type: fetch is not defined

Hello,
I am building a confluence app with anthropic-ai sdk.

By default, this library uses node-fetch in Node, and expects a global fetch function in other environments.

When I use the library I get the error below.

Error: Error thrown in the snapshot context.
App code snapshot error: Snapshot error occurred: Error: this environment is missing the following Web Fetch API type: fetch is not defined. You may need to use polyfills
Learn more about the snapshot context at https://go.atlassian.com/forge-snapshot-context. 

I have tried passing it forge’s fetch like so, but had no luck:

import { fetch } from "@forge/api";
const client = new Anthropic({
  fetch
  },
});

Any ideas?

What error are you getting when you are passing fetch?

Also, have you considered using the Node.js runtime (now in preview) which should be detected as a Node environment by the SDK, which should then use node-fetch automatically?

2 Likes

Thanks that worked.