Skip to main content
GET
/
api
/
image
/
:id
Get Image
curl --request GET \
  --url https://api.example.com/api/image/:id
Retrieve a cat image file by its unique identifier. This endpoint returns the actual image file that can be displayed in a browser or application.

Authentication

This endpoint requires Auth0 JWT authentication. Include the JWT token in the Authorization header:
Authorization: Bearer <your-token>

Request

id
number
required
The unique identifier of the image to retrieve.

Response

Returns the image file with the appropriate Content-Type header based on the file type (e.g., image/jpeg or image/png). The response body contains the raw binary image data.

Success Response (200)

The response will be the image file itself, which can be:
  • Displayed directly in an <img> tag
  • Downloaded as a file
  • Processed by image manipulation libraries

Error Responses

401 Unauthorized

Returned when the JWT token is missing or invalid.

404 Not Found

Returned when no image exists with the specified ID (will occur if the database record is not found).

500 Internal Server Error

Returned when the image record exists in the database but the file is missing from the filesystem.

Example Requests

cURL

curl -X GET https://api.example.com/api/image/42 \
  -H "Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9..." \
  --output cat-image.jpg

Using in HTML

<img src="https://api.example.com/api/image/42" 
     alt="Cat image" />
Note: When using in HTML, you’ll need to include the JWT token through other means (e.g., a proxy endpoint or query parameter if supported).

JavaScript Fetch

fetch('https://api.example.com/api/image/42', {
  headers: {
    'Authorization': `Bearer ${token}`
  }
})
  .then(response => response.blob())
  .then(blob => {
    const imageUrl = URL.createObjectURL(blob);
    document.querySelector('img').src = imageUrl;
  });

Implementation Details

The endpoint:
  1. Authenticates the request using the checkJwt middleware
  2. Extracts the numeric id from the URL path parameter
  3. Queries the database to get the image filename using getImage(id)
  4. Sends the file from the images/ directory using Express’s sendFile() method
The actual implementation from src/index.ts:70-74:
app.get('/api/image/:id', checkJwt, async (req, res) => {
  const id = Number(req.params.id);
  const fileInfo = await getImage(id);
  res.sendFile(path.join(imagesDir, fileInfo.name));
});

Build docs developers (and LLMs) love