sendwithus / sendwithus_nodejs

Sendwithus NodeJS Client
https://www.sendwithus.com
Apache License 2.0
22 stars 17 forks source link

CORS: 405 - method not allowed #40

Closed Sachin1678 closed 6 years ago

Sachin1678 commented 6 years ago

Getting below error: Failed to load https://api.sendwithus.com/api/v1/send: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8888' is therefore not allowed access. The response had HTTP status code 405. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Sample code:

const api = require('sendwithus')('test_xxxxxxxxxxxxxxxxxxxxxxxxx');

export function sendEmail() {
  debugger;
  api.send({
    template: 'tem_xxxxxxxxxxxx',
    recipient: { address: 'sample email address' },
    template_data: { url: 'https://www.google.com' },
    headers: { 'Access-Control-Allow-Origin': '*' } // also tried by removing it
  }, (response) => {
    console.log(response);
  });
}

export default {
  sendEmail
};

I am sending this request from web application(chrome). Also please let me know is it safe to keep API_KEY on frontend?

Version: 4.2.0 React: 16.2.0

dylan-swu commented 6 years ago

@Sachin1678 Good question! It is not safe to keep your API key available on the frontend. A malicious user could gain access to your Sendwithus account and start sending emails on your behalf.

That's why you're running into this error. This is expected behaviour as our node client is meant to be integrated server side.