FakeYou's API does not have public support for an API key so unless you have been given an API token by the owner, you're left with session authentication.
This guide will predominantly revolve around TypeScript, but in the end, we will be using HTTP as our transfer protocol. So long as your language works with HTTP, you should be able to translate it to your language of choice!
We will use session authentication, which means you need to send across a cookie to the API so that FakeYou recognises you.
That's it!
You will basically be replicating the exact technique the web browser uses when you log in to the official FakeYou platform.
You first need the cookie to authenticate! But how do we retrieve it?
You need to send a POST
request to https://api.fakeyou.com/v1/login
. The body will be the login credentials.
const response = await fetch("https://api.fakeyou.com/v1/login", {
method: "POST",
body: JSON.stringify({
username_or_email: usernameOrEmail,
password: password,
}),
});
const json: { success: boolean } = await response.json();
Then check it was successful:
if (!json.success) {
throw new Error("Authentication failed.");
}
And extract the cookie as a string:
const cookie = response.headers
.get("set-cookie")
?.match(/^\w+.=([^;]+)/)
?.at(1);
The above is a regular expression that extracts the cookie from the response headers. It's worked well for fakeyou.ts! Maybe you could simplify it.
Now just keep that cookie in your application ready subsequent requests.
Now you have the cookie, you should now include it in every subsequent request to authenticate.
const headers = new Headers();
headers.append("content-type", "application/json");
headers.append("credentials", "include"); // IMPORTANT! Your cookie will not be sent without this!
headers.append("cookie", `session=${cookie}`); // Add the cookie
const response = await fetch("https://api.fakeyou.com/endpoint", {
headers,
...options, // More options for the fetch
});
const json = await response.json();
Now, keep in mind the above example does not use a valid URL. Do check out the FakeYou docs for more information about what URLs you can leverage for your own TTS application.
As should be expected, your cookie will expire at some point in the future.
Your cookie will expire in exactly 7300 days (or just under 10 years).
So just make sure your cookie is never leaked. And use the /logout
endpoint to logout so that the cookie can no longer be used.
In summary, extract the cookie using the /v1/login
endpoint, then add it as a header for subsequent requests.