Let $f: X \to X$ be such that $d(f(x), f(y)) = d(x, y)$ for all $x, y \in X$. To show that $f$ is onto. Let $(X, d)$ be a compact metric space. Let $f: X \to X$ be such that $d(f(x), f(y)) = d(x, y)$ for all $x, y \in X$. To show that $f$ is onto.
Since the function $f$ satisfies $d(f(x), f(y)) = d(x, y)$ for all $x, y \in X$ we can say that the function is uniformly continuous.
Now let us assume that $f$ is not onto then $f(X) \subset X$, a proper subset. Then how can I bring a contraction to show that $f$ will be onto??

Let $x_0\in X$ and define
$$g:X\to\mathbb{R},\quad g(x)=d(f(x),x_0).$$
It is easy to show that $g$ is uniformly continuous. Since $X$ is compact, $g$ attains a minimum at some $y_0\in X$, i.e.
$$d(f(y_0),x_0)\leq d(f(x),x_0),\quad\forall x\in X.$$
We will show that this $y_0$ does it, i.e. $f(y_0)=x_0$. Suppose, for contradiction, that $f(y_0)\neq x_0$ and let
$$\epsilon_0:=d(f(y_0),x_0)>0$$
Define the sequence $(x_n)_{n=1}^\infty$ inductively by
$$x_1=f(x_0),\quad x_{n+1}=f(x_n).$$
Then, for any $m,n\in\mathbb{N}$, $m>n$, we have
\begin{align*}
d(x_m,x_n)&=d(f(x_{m-1}),f(x_{n-1}))=d(x_{m-1},x_{n-1})=\cdots=d(x_{m-n},x_0) \\
&=d(f(x_{m-n-1}),x_0)\geq d(f(y_0),x_0)=\epsilon_0
\end{align*}
Thus, $(x_n)_{n=1}^\infty$ cannot have any Cauchy subsequence. This contradicts that $X$ is compact, since a compact metric space is sequentially compact. So $f(y_0)=x_0$, and hence $f$ is onto.
