-2

I am facing CORS issue while integrating 'https://api.anthropic.com/v1/messages',

Does anyone solved it?

try {
      const response = await axios.post(
        'https://api.anthropic.com/v1/messages',
        {
          model: "claude-3-5-sonnet-20240620",
          max_tokens: 1024,
          messages: [
            { "role": "user", "content": "one word" }
          ]
        },
        {
          headers: {
            'x-api-key': 'APIKey',
            'anthropic-version': '2023-06-01',
            'content-Type': 'application/json',
          },
        }
      );
      console.log('Model used:', response.data.model);
      return response?.data?.choices[0]?.message?.content?.trim();
    } catch (error) {
      console.error('Error:', error.response ? error.response.data : error.message);
      throw error;
    }
    
1
  • You're out of luck. According to my tests, this resource's CORS configuration doesn't allow clients from arbitrary Web origins. You'll have to consume the resource from a client that's not browser-based. Commented Jul 13, 2024 at 15:54

1 Answer 1

1

Using this API directly in the ReactJS is supposed to throw CORS error for the security. In this method your API keys will be exposed to the end user who's going to use your application.

The better and safe approach to do this is to create a backend proxy which will attach your API key in the headers and call the anthropic API on your behalf.

Sign up to request clarification or add additional context in comments.

1 Comment

This is my plan B but would like to make it simple like how ChatGPT handled CORS to their end.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.