anthropics / anthropic-sdk-typescript

Access to Anthropic's safety-first language model APIs
https://www.npmjs.com/package/@anthropic-ai/sdk
MIT License
658 stars 71 forks source link

CORS: Message API return CORS issues. #410

Closed firllyw closed 4 months ago

firllyw commented 4 months ago
image

Step to Reproduce:

`const anthropic = new Anthropic({ apiKey: process.env.NEXT_PUBLIC_ANTHROPIC_API_KEY, });

function MyDiagrams() { const [loading, setLoading] = useState(false);

const askClaude = async (question) => {
    setLoading(true);

    const msg = await anthropic.messages.create({
        model: "claude-3-opus-20240229",
        max_tokens: 4000,
        temperature: 0,
        system: "some prompt engineering here",
        messages: [
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": question
                    }
                ]
            }
        ]
    })

    if (!msg) {
        toast.error('Failed to get response from Claude');
        setLoading(false);
        return;
    }

    setLoading(false);
}

export default MyDiagrams;
`

snippet above is from a Page.js . 

Live app: https://flowy-ai.netlify.app/dashboard/my-diagrams/create
BrandonStudio commented 4 months ago

Before sending POST request, broswer will first send an OPTIONS preflight request to API (e.g. https://api.anthropic.com/v1/messages). But right now Anthropic API endpoint does not allow OPTIONS method.

aaronvg commented 4 months ago

Also running into this issue. Anthropic APIs are not callable from the client-side without a proxy server

cwray-sdc commented 4 months ago

Also seeing this issue when calling from localhost.

rattrayalex commented 4 months ago

Dup of https://github.com/anthropics/anthropic-sdk-typescript/issues/219

x5a commented 1 month ago

@firllyw + thread: our latest SDK release now offers CORS support [changelog].

EliasPereirah commented 18 hours ago

Any updates on this? I'm creating an interface but it doesn't work for Anthropic due to this error. https://github.com/EliasPereirah/OrionChat/