Copicake JS
🍰 Copicake, a data-driven image generating service to let you generate any social media material with just ONE CLICK.
Installations
Because we host our library on GitHub, so you need to change your settings to use our library.
For npm user
Change your .npmrc
file to :
registry=https://registry.npmjs.org
@copicake:registry=https://npm.pkg.github.com
And then install copicake:
npm install --save @copicake/copicake-js
For yarn user
Add the following settings into your .yarnrc
file:
registry "https://registry.npmjs.org"
"@copicake:registry" "https://npm.pkg.github.com"
And then install copicake:
yarn add @copicake/copicake-js --dev
Usage
Initialization
You need to initialize to get copicake instance first :
import Copicake from "@copicake/copicake-js";
const copicake = new Copicake({
apiKey: "your-api-key",
});
Create an image
copicake.image
.create({
template_id: "YOU_TEMPLATE_ID",
changes: [
{ name: "text-9so09m", text: "hello world", color: "#ff0000" },
{ name: "image-yeavh7", src: "https://your_website.com/test.png" },
],
options: {
webhook_url: "https://your_website.com/webhook_url",
},
})
.then((response) => {
console.log(response);
})
.catch((error) => {
console.error(error);
});
Get an image
const renderingId = `YOUR_RENDERING_ID`;
copicake.image
.get(renderingId)
.then((response) => {
console.log(response);
})
.catch((error) => {
console.error(error);
});
Get an image (long polling)
Sometimes you may notice that your image is still under processing
state, this is because the image is still being processed in the background by our servers.
In this way, we provide another handy method called getUntilFinished()
to get the image until the image is ready.
Internally, this is just a wrapper of get()
method with built-in retry mechanism. If after MAX_RETRY_TIMES and the image is still under processing
state, we will throw an error (500) to let you know.
const renderingId = `YOUR_RENDERING_ID`;
copicake.image
.getUntilFinished(renderingId)
.then((response) => {
if (response.status === "success") {
} else if (response.status === "failed") {
}
})
.catch((error) => {
console.error(error);
});