Skip to content

🎥 Make videos programmatically with React

add documentation

Hunain Ahmedb6c96d9f081f
diff --git a/packages/docs/components/TableOfContents/lambda.tsx b/packages/docs/components/TableOfContents/lambda.tsx
index e62c4d65ed..88e9d79324 100644
--- a/packages/docs/components/TableOfContents/lambda.tsx
+++ b/packages/docs/components/TableOfContents/lambda.tsx
@@ -94,6 +94,14 @@ export const TableOfContents: React.FC = () => {
<strong>validateWebhookSignature()</strong>
<div>Validate an incoming webhook request is authentic</div>
</TOCItem>
+ <TOCItem link="/docs/lambda/approuterwebhooks">
+ <strong>appRouterWebhooks()</strong>
+ <div>Handle incoming webhooks specifically for the Next.js app router</div>
+ </TOCItem>
+ <TOCItem link="/docs/lambda/pagerouterwebhooks">
+ <strong>pageRouterWebhooks()</strong>
+ <div>Handle incoming webhooks specifically for the Next.js pages router</div>
+ </TOCItem>
</Grid>
</div>
);
diff --git a/packages/docs/docs/lambda/approuterwebhooks.mdx b/packages/docs/docs/lambda/approuterwebhooks.mdx
new file mode 100644
index 0000000000..162caa5a37
--- /dev/null
+++ b/packages/docs/docs/lambda/approuterwebhooks.mdx
@@ -0,0 +1,71 @@
+---
+image: /generated/articles-docs-lambda-approuterwebhooks.png
+id: approuterwebhooks
+title: appRouterWebhooks()
+slug: /lambda/approuterwebhooks
+crumb: "Lambda API"
+---
+
+Simplifies the process of setting up Remotion Lambda Client in your Next.js app which is using App Router. Refer to [pagesRouterWebhooks()](/docs/lambda/pagerouterwebhooks) for doing the same in apps using Pages Router.
+## API
+
+The function accepts an object with six key-value pairs:
+
+### `secret`
+
+Your webhook secret, must be a `string`
+
+### `testing`
+
+Whether or not to allow requests intending to test the endpoint, useful while using Webhook endpoint tester on [Webhooks Page](/docs/lambda/webhooks). Should be a `boolean`.
+
+### `extraHeaders`
+
+Add your own custom headers to the outgoing response. Provide key-value pairs where both the key and value are strings.
+
+
+### `onSuccess()`
+
+A function that is called with a `Payload` object as an argument when the incoming request indicates a successful event.
+
+### `onError()`
+
+A function that is called with a `Payload` object as an argument when the incoming request indicates an error.
+
+### `onTimeout()`
+
+A function that is called with a `Payload` object as an argument when the incoming request indicates a timeout.
+
+
+## Example
+
+Setting up a webhook endpoint in a Next.js app which uses App Router. This will listen on the endpoint: `mydomain.com/api`
+
+
+```tsx twoslash title="app/api/route.ts"
+import {
+ appRouterWebhooks
+} from '@remotion/lambda/client';
+
+export const POST = appRouterWebhooks(
+ {
+ secret: 'mysecret',
+ testing: true,
+ extraHeaders: {
+ "region" : 'south-asia'
+ },
+ onSuccess: () => (console.log("Rendering Completed Successfully")),
+ onError: () => (console.log("Something went wrong while rendering")),
+ onTimeout: () => (console.log("Timeout occured while rendering"))
+ }
+)
+
+export const OPTIONS = POST;
+```
+
+See [Webhooks](/docs/lambda/webhooks) for an Express example.
+
+## See also
+
+- [Webhooks](/docs/lambda/webhooks)
+- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/lambda/src/simplifiers/index.ts)
diff --git a/packages/docs/docs/lambda/pagerouterwebhooks.mdx b/packages/docs/docs/lambda/pagerouterwebhooks.mdx
new file mode 100644
index 0000000000..5f006f3427
--- /dev/null
+++ b/packages/docs/docs/lambda/pagerouterwebhooks.mdx
@@ -0,0 +1,68 @@
+---
+image: /generated/articles-docs-lambda-pagerouterwebhooks.png
+id: pagerouterwebhooks
+title: pageRouterWebhooks()
+slug: /lambda/pagerouterwebhooks
+crumb: "Lambda API"
+---
+
+Simplifies the process of setting up Remotion Lambda Client in your Next.js app which is using Pages Router. Similar to [appRouterWebhooks()](/docs/lambda/approuterwebhooks).
+## API
+
+The function accepts an object with six key-value pairs:
+
+### `secret`
+
+Your webhook secret, must be a `string`
+
+### `testing`
+
+Whether or not to allow requests intending to test the endpoint, useful while using Webhook endpoint tester on [Webhooks Page](/docs/lambda/webhooks). Should be a `boolean`.
+
+### `extraHeaders`
+
+Add your own custom headers to the outgoing response. Provide key-value pairs where both the key and value are strings.
+
+
+### `onSuccess()`
+
+A function that is called with a `Payload` object as an argument when the incoming request indicates a successful event.
+
+### `onError()`
+
+A function that is called with a `Payload` object as an argument when the incoming request indicates an error.
+
+### `onTimeout()`
+
+A function that is called with a `Payload` object as an argument when the incoming request indicates a timeout.
+
+## Example
+
+Setting up a webhook endpoint in a Next.js app which uses Pages Router. This will listen on the endpoint: `mydomain.com/api/webhook`
+
+```tsx twoslash title="pages/api/webhook.ts"
+import {
+ pageRouterWebhooks
+} from '@remotion/lambda/client';
+
+const handler = pageRouterWebhooks(
+ {
+ secret: 'mysecret',
+ testing: true,
+ extraHeaders: {
+ "region" : 'south-asia'
+ },
+ onSuccess: () => (console.log("Rendering Completed Successfully")),
+ onError: () => (console.log("Something went wrong while rendering")),
+ onTimeout: () => (console.log("Timeout occured while rendering"))
+ }
+)
+export default handler
+```
+
+See [Webhooks](/docs/lambda/webhooks) for an Express example.
+
+## See also
+
+- [Webhooks](/docs/lambda/webhooks)
+- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/lambda/src/simplifiers/index.ts)
diff --git a/packages/docs/docs/lambda/webhooks.mdx b/packages/docs/docs/lambda/webhooks.mdx
index e000deaf5b..8c77725b11 100644
--- a/packages/docs/docs/lambda/webhooks.mdx
+++ b/packages/docs/docs/lambda/webhooks.mdx
@@ -205,122 +205,41 @@ router.post("/my-remotion-webhook-endpoint", jsonParser, (req, res) => {
## Example webhook endpoint (Next.JS App Router)
-Similary, here is an example endpoint in Next.JS for the App Router.
-
-Since this endpoint is going to be executed in an AWS Lambda function on its own, you want to import the Remotion functions from [`@remotion/lambda/client`](/docs/lambda/light-client).
+Similary, here is an example endpoint in Next.JS for the App Router using [appRouterWebhooks()](/docs/lambda/approuterwebhooks)
```tsx twoslash title="app/api/webhook.ts"
import {
- validateWebhookSignature,
- WebhookPayload,
+ appRouterWebhooks
} from '@remotion/lambda/client';
-
-// Enable testing through the tool below
-// You may disable it in production
-const ENABLE_TESTING = true;
-
-export const POST = async (req: Request, res: Response) => {
- let headers = {};
-
- if (ENABLE_TESTING) {
- const testingheaders = {
- 'Access-Control-Allow-Origin': 'https://www.remotion.dev',
- 'Access-Control-Allow-Headers':
- 'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version, X-Remotion-Status, X-Remotion-Signature, X-Remotion-Mode',
- 'Access-Control-Allow-Methods': 'OPTIONS,POST',
- };
- headers = {...headers, ...testingheaders};
- }
-
- if (req.method === 'OPTIONS') {
- return new Response(null, {
- headers,
- });
+
+export const POST = appRouterWebhooks(
+ {
+ secret: 'mysecret',
+ testing: true,
+ onTimeout: ()=>(console.log("timeout"))
}
-
- // Parse the body properly
- const body = await req.json();
-
- validateWebhookSignature({
- secret: process.env.WEBHOOK_SECRET as string,
- body: body,
- signatureHeader: req.headers.get('X-Remotion-Signature') as string,
- });
-
- const payload = body as WebhookPayload;
-
- if (payload.type === 'success') {
- //...
- } else if (payload.type === 'timeout') {
- //...
- }
-
- return new Response(JSON.stringify({success: true}));
-};
-
+)
+
export const OPTIONS = POST;
```
## Example webhook endpoint (Next.JS Pages Router)
-The same endpoint as above, but using the Pages Router.
+The same endpoint as above, but using the Pages Router with the help of [pageRouterWebhooks()](/docs/lambda/pagerouterwebhooks)
```tsx twoslash title="pages/api/webhook.ts"
-type NextApiRequest = {
- body: object;
- headers: Record<string, string>;
- method: string;
-};
-type NextApiResponse = {
- status: (code: number) => {json: (body: object) => void; end: () => void};
- setHeader: (key: string, value: string) => void;
- end: () => void;
-};
-// ---cut---
import {
- validateWebhookSignature,
- WebhookPayload,
+ pageRouterWebhooks
} from '@remotion/lambda/client';
-// Enable testing through the tool below
-// You may disable it in production
-const ENABLE_TESTING = true;
-
-export default async function handler(
- req: NextApiRequest,
- res: NextApiResponse,
-) {
- if (ENABLE_TESTING) {
- res.setHeader('Access-Control-Allow-Origin', 'https://www.remotion.dev');
- res.setHeader('Access-Control-Allow-Methods', 'OPTIONS,POST');
- res.setHeader(
- 'Access-Control-Allow-Headers',
- 'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version, X-Remotion-Status, X-Remotion-Signature, X-Remotion-Mode',
- );
+const handler = pageRouterWebhooks(
+ {
+ secret: 'mysecret',
+ testing: true,
+ onError: () => (console.log('Something went wrong'))
}
- if (req.method === 'OPTIONS') {
- res.status(200).end();
- return;
- }
-
- validateWebhookSignature({
- secret: process.env.WEBHOOK_SECRET as string,
- body: req.body,
- signatureHeader: req.headers['x-remotion-signature'] as string,
- });
-
- // If code reaches this path, the webhook is authentic.
- const payload = req.body as WebhookPayload;
- if (payload.type === 'success') {
- // ...
- } else if (payload.type === 'timeout') {
- // ...
- }
-
- res.status(200).json({
- success: true,
- });
-}
+)
+export default handler
```
## Test your webhook endpoint
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 2571bbd442..73b77483e5 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -250,6 +250,8 @@ module.exports = {
'lambda/simulatepermissions',
'lambda/speculatefunctionname',
'lambda/validatewebhooksignature',
+ 'lambda/approuterwebhooks',
+ 'lambda/pagerouterwebhooks'
],
},
{