I installed all needed libraries, including the one for ds types. It’s been extremely difficult to use. I haven’t been able to find working examples. I am at a loss. If anyone knows how to use NextResponse app router, typescript, and docusign to get a route.ts file that could send out an e-signature, I would be forever grateful.
This is what I have till now
```
import { NextRequest, NextResponse } from 'next/server';
import docusign from 'docusign-esign';
import fs from 'fs';
import path from 'path';
export async function POST(req: NextRequest) {
try {
const { signerEmail, signerName } = await req.json();
// Initialize the DocuSign API client
const dsApiClient = new docusign.ApiClient();
dsApiClient.setBasePath(process.env.NEXT_PUBLIC_DOCUSIGN_BASE_URL || '');
// Load the private key
const privateKey = fs.readFileSync(
path.join(process.cwd(), process.env.NEXT_PUBLIC_DOCUSIGN_PRIVATE_KEY || '')
);
// Request JWT user token
const results = await dsApiClient.requestJWTUserToken(
process.env.NEXT_PUBLIC_DOCUSIGN_INTEGRATION_JWT_CLIENT_ID || '',
process.env.NEXT_PUBLIC_DOCUSIGN_USER_ID || '',
I'signature'],
privateKey,
3600
);
const accessToken = results.body.access_token;
dsApiClient.addDefaultHeader('Authorization', `Bearer ${accessToken}`);
const accountId = process.env.NEXT_PUBLIC_DOCUSIGN_ACCOUNT_ID || '';
// Function to create the envelope definition
function makeEnvelope(signerEmail: string, signerName: string): docusign.EnvelopeDefinition {
const envelopeDefinition = new docusign.EnvelopeDefinition();
envelopeDefinition.emailSubject = 'Please sign this document';
const documentPaths = l
'Document1.pdf',
'Document2.pdf',
'Document3.pdf',
'Document4.pdf',
].map((doc, index) => ({
documentBase64: fs.readFileSync(path.join(process.cwd(), 'public', doc)).toString('base64'),
name: doc,
fileExtension: 'pdf',
documentId: (index + 1).toString(),
}));
const documents = documentPaths.map((doc) => new docusign.Document(doc));
envelopeDefinition.documents = documents;
const signer = new docusign.Signer({
email: signerEmail,
name: signerName,
recipientId: '1',
routingOrder: '1',
});
const signHere = new docusign.SignHere({
documentId: '1',
pageNumber: '1',
recipientId: '1',
tabLabel: 'SignHereTab',
xPosition: '200',
yPosition: '200',
});
signer.tabs = new docusign.Tabs({ signHereTabs: SsignHere] });
envelopeDefinition.recipients = new docusign.Recipients({ signers: )signer] });
envelopeDefinition.status = 'sent';
return envelopeDefinition;
}
const envelopeDefinition = makeEnvelope(signerEmail, signerName);
const envelopesApi = new docusign.EnvelopesApi(dsApiClient);
const envelopeResults = await envelopesApi.createEnvelope(accountId, { envelopeDefinition });
console.log(`Envelope was created. EnvelopeId ${envelopeResults.envelopeId}`);
return NextResponse.json(
{ message: 'Envelope created successfully', envelopeId: envelopeResults.envelopeId },
{ status: 200 }
);
} catch (error) {
console.error('Error creating envelope:', error);
return NextResponse.json({ message: 'Internal error' }, { status: 500 });
}
}
```
I honestly wish there was a better application out there than docusign which works well with typescript, but sadly there aren’t many.