With the widespread use of electric vehicles, the rapid development of power electronics and smart grids making more and more nonlinear devices connected to the grid, the power quality problem is becoming more and more prominent, and active power filters (APFs) are widely used to reduce the harmonic voltage and current in the grid. In this paper, the optimal configuration and dimensions of APFs for distribution networks based on the improved Beluga whale optimization (IBWO) are proposed. The IBWO algorithm proposed in this paper is characterized by fast convergence and high accuracy. The objective of the method proposed in this paper is to reduce the number of APFs used and the investment cost, while making the total voltage harmonic distortion rate (VTHD) and the individual VTHD of the grid meet the requirements. Finally, this paper verifies the effectiveness and feasibility of the proposed method by an example in an IEEE-18 bus test system, and the results show that the proposed method is effective in optimizing the location and capacity of multiple APFs.