# Building an HTTPS-Intercepting JavaScript Proxy with Mockttp

DevFeed: [Building an HTTPS-Intercepting JavaScript Proxy with Mockttp](<https://devfeed.tech/articles/build-an-https-intercepting-javascript-proxy-in-30-seconds-flat-19082.md>)

Original publisher: [Read original article](<https://httptoolkit.com/blog/javascript-mitm-proxy-mockttp/>)

Author: HTTP Toolkit; Tim Perry

Published: 2021-04-27T14:00:00Z

Content type: tutorial

Language: en

Sources: [HTTP Toolkit](<https://devfeed.tech/sources/http-toolkit.md>)

Topics: [HTTP](<https://devfeed.tech/topics/http.md>), [JavaScript](<https://devfeed.tech/topics/javascript.md>)

Tags: [http](<https://devfeed.tech/tags/http.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [open-source](<https://devfeed.tech/tags/open-source.md>), [proxy](<https://devfeed.tech/tags/proxy.md>), [testing](<https://devfeed.tech/tags/testing.md>)

## AI overview

A tutorial showing how to use the Mockttp JavaScript library to create an HTTPS-intercepting proxy that can mock and modify HTTP traffic.

## Source excerpt

HTTP(S) is the glue that binds together modern architectures, passing requests between microservices and connecting web & mobile apps alike to the APIs they depend on. What if you could embed scripts directly into that glue? By doing so, you could: Inject errors, timeouts and unusual responses to test system reliability. Record & report traffic from all clients for later analysis. Redirect requests to replace production servers with local test servers. Automatically validate and debug HTTP interactions across an entire system. It turns out setting this up is super quick & easy to do. Using easily available JS libraries and scripts, you can start injecting code into HTTP interactions in no time at all. Let's see how it works. Putting the basics together Mockttp is the open-source HTTP library that powers all the internals of HTTP Toolkit, built in TypeScript. It can act as an HTTP(S) server or proxy, to intercept and mock traffic, transform responses, inject errors, or fire events for all the traffic it receives. First though, if you want to inspect & edit HTTP manually with a full UI and tools on top, it's better to download HTTP Toolkit for free right now instead, and start there! On the other hand, if you do want to build scripts and automations that capture & rewrite HTTPS, or if you've used HTTP Toolkit and now you want to create complex custom behaviour on top of its built-in rules, then Mockttp is perfect, and you're in the right place. Getting started with Mockttp is easy: install it, define a server, and start it. That looks like this: Create a new directory Run npm install mockttp Create an index.js script: (async () => { const mockttp = require('mockttp');// Create a proxy server with a self-signed HTTPS CA certificate: const https = await mockttp.generateCACertificate(); const server = mockttp.getLocal({ https }); // Inject 'Hello world' responses for all requests server.forAnyRequest().thenReply(200, "Hello world"); await server.start(); // Print out the