API Request fails every time

Hi!
I tried to use your api to create a room.
I use your example code above with my credentials:

fetch("https://w2g.tv/rooms/create.json", {
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        "w2g_api_key": "<api_key>",
        "share": "https://www.youtube.com/watch?v=8Wdp35Z-fRs",
        "bg_color": "#00ff00",
        "bg_opacity": "50"
    })
})
.then(response => response.json())
.then(function (data) {
    console.log("W2G: Here is your room! \n https://w2g.tv/rooms/" + data.streamkey);
});

But every time I try to run it, it returns me this exception:

Failed to load resource: net::ERR_FAILED

Does anyone know why this happens?
Thanks!

Do you know which HTTP status code you get in the response?

Hi!

All I get are these errors:

1:
Access to fetch at 'https://w2g.tv/rooms/create.json' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow- Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. w2g.tv/rooms/create.json:1

2:
Failed to load resource: net::ERR_FAILED index.html:4

3:
Uncaught (in promise) TypeError: Failed to fetch at index.html:4

I don’t know which status code you are talking about, but:
If I go on the website https://w2g.tv/rooms/create.json normally with a normal browser, without fetching anything, it says to me: “Internal Server Error 500”
(I don’t know if that has any meaning)

Thanks for the info. It sounds like your are trying to run the code in a browser. This won’t work. The API is intended to be used only on a server. If you want to use JavaScript you can use NodeJS on your server.

1 Like

Is this still the case? I’m writing a chrome extension so I can right click a youtube link to auto add to the current playlist in the specified room. Is there updated api documentation?

Hi! Yes if you want to do that you should create a small server component that handles the API requests for your extension.

Are there any projects that already do this that you can point me to? New to this. thanks

I would start with a simple nodejs server using the express framework https://expressjs.com/. You call your server from your extension and the server makes the API requests to Watch2Gether. You might find some sample code on Github if you search for Watch2Gether. Using ChatGPT or similar should also produce some sample code that you can base your project on.