Skip to content

TwimlInterceptor

Defined in: lib/twiml/twiml.interceptor.ts:198

Serializes Twilio TwiML builder responses (VoiceResponse, MessagingResponse, FaxResponse) returned from a controller handler into their XML string representation, and sets the matching Content-Type header on the underlying HTTP response.

Non-TwiML return values are passed through unchanged, so the interceptor is safe to register globally alongside handlers that return plain JSON. It works identically on the Express and Fastify platform adapters, since it only relies on the response.header() method both implement, and it never touches the response status code or any other header.

Registered globally, using the default content type

app.useGlobalInterceptors(new TwimlInterceptor());

Registered per-route, overriding the default content type

@Post('incoming')
@UseInterceptors(new TwimlInterceptor({ contentType: 'application/xml' }))
handleIncomingCall(): MessagingResponse {
const response = new MessagingResponse();
response.message('Thanks for texting us!');
return response;
}

Registered as a provider, with a per-route override

providers: [{ provide: APP_INTERCEPTOR, useClass: TwimlInterceptor }];
  • NestInterceptor
new TwimlInterceptor(options?): TwimlInterceptor;

Defined in: lib/twiml/twiml.interceptor.ts:209

Parameter Type
options TwimlInterceptorOptions

TwimlInterceptor

intercept(context, next): Observable<unknown>;

Defined in: lib/twiml/twiml.interceptor.ts:217

Method to implement a custom interceptor.

Parameter Type Description
context ExecutionContext an ExecutionContext object providing methods to access the route handler and class about to be invoked.
next CallHandler a reference to the CallHandler, which provides access to an Observable representing the response stream from the route handler.

Observable<unknown>

NestInterceptor.intercept