Setup
- JavaScript
- Python
- Node.js
- C++
- Rust
That JavaScript library is being extensively revised so the APIs used here may change in the near future.
This is how to set up your code to use the JavaScript library:
const version = '0.27.1';
const sampleImage = '<IMAGE_URL>';
import { createC2pa } from 'https://cdn.jsdelivr.net/npm/c2pa@${version}/+esm';
(async () => {
// Initialize the c2pa-js SDK
const c2pa = await createC2pa({
wasmSrc:
'https://cdn.jsdelivr.net/npm/c2pa@${version}/dist/assets/wasm/toolkit_bg.wasm',
workerSrc:
'https://cdn.jsdelivr.net/npm/c2pa@${version}/dist/c2pa.worker.min.js',
});
...
})();
If you installed the package locally, for example from npm, then it's simply:
import { createC2pa } from 'c2pa';
Additionally, the wasmSrc
and workerSrc
objects need to available as static assets that can be fetched at runtime. The best way to do that depends on the build system. For example, using Vite, as shown in the minimal-ts-vite example:
import wasmSrc from 'c2pa/dist/assets/wasm/toolkit_bg.wasm?url';
import workerSrc from 'c2pa/dist/c2pa.worker.js?url';
import { parseISO } from 'date-fns';
(async () => {
let output: string[] = [];
const c2pa = await createC2pa({
wasmSrc,
workerSrc,
});
...
})();
This is how to set up your code to use the Python library.
# Import the C2PA Python package.
from c2pa import *
# Import standard general-purpose packages.
import os
import io
import logging
import json
import base64
# Import web packages used in example implementation.
from flask import Flask, request, abort
from flask_cors import CORS
from waitress import serve
# Import AWS SDK package (to use KMS).
import boto3
The Node.js library is being revised. The documentation will be updated as soon as possible with the latest changes.
This is how to set up your code to use the C++ library:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdexcept>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include "c2pa.hpp"
#include "test_signer.hpp"
#include <nlohmann/json.hpp>
// this example uses nlohmann json for parsing the manifest
using json = nlohmann::json;
using namespace std;
namespace fs = std::filesystem;
using namespace c2pa;
This is how to setup your code to use the Rust library.
The files used in this example are in the C2PA Rust library sdk/tests/fixtures
directory.
use std::{
io::{Cursor, Write},
process::{Command, Stdio},
};
use anyhow::Result;
use c2pa::{settings::load_settings_from_str, Builder, CallbackSigner, Reader};
use c2pa_crypto::raw_signature::SigningAlg;
use serde_json::json;
const TEST_IMAGE: &[u8] = include_bytes!("../tests/fixtures/CA.jpg");
const CERTS: &[u8] = include_bytes!("../tests/fixtures/certs/ed25519.pub");
const PRIVATE_KEY: &[u8] = include_bytes!("../tests/fixtures/certs/ed25519.pem");