API mocks are useful for simulating an API endpoint. For example, when building a front end while the backend API is under construction and unstable, Insomnia allows us to customize responses from a set of API paths to simulate a static API. This mocked URL can then replace our front-end API backend URL.
From the request collection screen, on the response pane to the right, we can use the Mock Response tab to extract the most recent response and API path from the currently selected request to build an API mock.
This feature is a time-saver when we already have our response structures within Insomnia that we would like to make available from our API mock.
Any logged-in user can create API mocks, accessible at mock.insomnia.rest. These mocks can be collaborated on with a team and accessed by anyone.
As a consumer of this mock server we can modify our environment (e.g. BACKEND_API_URL
) to point at the Insomnia Cloud Mock server.
// Example .env file for your front-end application
BACKEND_API_URL=https://foobar.mock.insomnia.rest/
THIRD_PARTY_API_KEY=foo
Enterprise plans have access to self-hosted mocks, providing increased control over usage.
Configuration details can be found at GitHub - Kong Insomnia Mockbin and a Docker image is available at ghcr.io/kong/insomnia-mockbin:master.
Instructions for running locally with Docker or NodeJS can be found on GitHub:
// Example .env file for your front-end application
BACKEND_API_URL=https://localhost:8080/bin/foobar
THIRD_PARTY_API_KEY=foo
kubectl
command-line tool configured to interact with your cluster.Create and modify the Deployment File: Create/Open deployment.yaml
and make sure to replace any placeholder values with actual data. You might need to set values for the number of replicas, resource limits, and Mockbin-specific environment variables.
apiVersion: apps/v1
kind: Deployment
metadata:
name: insomnia-mock
namespace: mock
spec:
replicas: 1 # Set the number of replicas
selector:
matchLabels:
app: insomnia-mock
template:
metadata:
labels:
app: insomnia-mock
spec:
containers:
- name: insomnia-mock
image: ghcr.io/kong/insomnia-mockbin:master
ports:
- containerPort: 9080
env:
- name: MOCKBIN_PORT
value: "9080"
# Set other environment variables as required
Deploy Mockbin: Run the following command to create the deployment in Kubernetes:
kubectl apply -f deployment.yaml
Configure the Service: Create/Open service.yaml
. This file configures the service to expose Mockbin internally in the cluster.
apiVersion: v1
kind: Service
metadata:
name: insomnia-mock
namespace: mock
spec:
type: ClusterIP
ports:
- name: mock
port: 9080
targetPort: 9080
selector:
app: insomnia-mock
Deploy the Service: Run the following command:
kubectl apply -f service.yaml
Configure the Ingress: Create/Edit ingress.yaml
to manage external access. This configuration will depend heavily on your specific domain and TLS requirements.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: insomnia-mock-ingress
namespace: mock
spec:
ingressClassName: nginx
rules:
- host: mock.your-domain.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: insomnia-mock
port:
number: 9080
tls:
- hosts:
- mock.your-domain.com
secretName: insomnia-mock-tls
Deploy the Ingress: Replace mock.your-domain.com
with your actual domain and update the TLS secret names accordingly. Then, apply the configuration:
kubectl apply -f ingress.yaml
Check the Deployment Status:
kubectl get deployments -n mock
Check the Service:
kubectl get services -n mock
Check the Ingress:
kubectl get ingress -n mock
Edit your your front-end application .env
to point to the Ingress host:
// Example .env file for your front-end application
BACKEND_API_URL=https://mock.your-domain.com/bin/foobar
THIRD_PARTY_API_KEY=foo