Skip to content

Instantly share code, notes, and snippets.

@kerolloz
Created July 5, 2024 15:39
Show Gist options
  • Save kerolloz/679c9053bd7dbeea4a60cdf493e819ed to your computer and use it in GitHub Desktop.
Save kerolloz/679c9053bd7dbeea4a60cdf493e819ed to your computer and use it in GitHub Desktop.
Upload an image to ImgHippo for FREE
import fs from 'node:fs';
import axios, { AxiosError } from 'axios';
import formData from 'form-data';
type ImgHippoResponse = { data: { view_url: string } };
/**
* Uploads an image to ImgHippo and returns the URL of the uploaded image.
* @example await uploadImage('path/to/image.png'); // https://i.imghippo.com/files/VGcQE1720193383.png
* @param imagePath the path to the image to upload
* @returns the URL of the uploaded image
*/
export const uploadImage = async (imagePath: string): Promise<string> => {
const imgUploadApiKeyKeyEnvVar = process.env.IMG_UPLOAD_API_KEY;
const form = new formData();
form.append('file', fs.createReadStream(imagePath));
try {
const { data } = await axios.post<ImgHippoResponse>(
`https://www.imghippo.com/v1/upload?api_key=${imgUploadApiKeyKeyEnvVar}`,
form,
);
return data.data.view_url;
} catch (error) {
console.error(error instanceof AxiosError ? error.response?.data : error);
throw new Error('Error uploading image');
}
};
@veilofsecurity
Copy link

API is fine, the code snippet is incorrect. api_key is not a parameter but is a form key\value pair.
curl -L 'https://api.imghippo.com/v1/upload' \ -F 'api_key="<apikey>"' \ -F 'title="imageTitle"' \ -F 'file=@"<path_to_file>"'

@sandaruDevr
Copy link

its still not working

@veilofsecurity
Copy link

This is likely because you are getting blocked by Cloudflare. The API calls are really hit or miss because Cloudflare presents a "confirm you're human" checkbox. I moved on from ImgHippo because the API is unreliable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment