API key issue when creating GPT Action using Google Map Directions API

I am a starter and I am creating a custom GPT with GPT Action calling Google Map Directions API.

I have created Google Cloud Project, enabled the Directions API and successfully got my API key.

When I created the GPT action, here is my OpenAI schema:

openapi: 3.1.0
info:
  title: Google Maps Directions API
  description: An API to get directions between locations, supporting multiple modes of transportation and route optimization.
  version: 1.0.0
servers:
  - url: https://maps.googleapis.com/maps/api/directions
    description: Google Maps Directions API server
paths:
  /json:
    get:
      operationId: getDirections
      summary: Get directions between locations
      parameters:
        - name: key
          in: query
          required: true
          schema:
            type: string
          description: Your API key
        - name: origin
          in: query
          required: true
          schema:
            type: string
          description: The starting point for directions
        - name: destination
          in: query
          required: true
          schema:
            type: string
          description: The endpoint for directions
        - name: mode
          in: query
          required: false
          schema:
            type: string
            enum: [driving, walking, bicycling, transit]
          description: Mode of transport
        - name: waypoints
          in: query
          required: false
          schema:
            type: string
          description: One or more intermediary points to pass through
        - name: alternatives
          in: query
          required: false
          schema:
            type: boolean
          description: Whether to provide alternative routes
        - name: avoid
          in: query
          required: false
          schema:
            type: string
            enum: [tolls, highways, ferries, indoor]
          description: Features to avoid in the route
        - name: language
          in: query
          required: false
          schema:
            type: string
          description: The language in which to return results
        - name: region
          in: query
          required: false
          schema:
            type: string
          description: The region code, specified as a ccTLD ("top-level domain") two-character value
      responses:
        '200':
          description: A JSON object containing the directions
          content:
            application/json:
              schema:
                type: object
                properties:
                  geocoded_waypoints:
                    type: array
                    items:
                      type: object
                      properties:
                        geocoder_status:
                          type: string
                        place_id:
                          type: string
                        types:
                          type: array
                          items:
                            type: string
                  routes:
                    type: array
                    items:
                      type: object
                      properties:
                        summary:
                          type: string
                        legs:
                          type: array
                          items:
                            type: object
                            properties:
                              distance:
                                type: object
                                properties:
                                  text:
                                    type: string
                                  value:
                                    type: integer
                              duration:
                                type: object
                                properties:
                                  text:
                                    type: string
                                  value:
                                    type: integer
                              end_address:
                                type: string
                              end_location:
                                type: object
                                properties:
                                  lat:
                                    type: number
                                  lng:
                                    type: number
                              start_address:
                                type: string
                              start_location:
                                type: object
                                properties:
                                  lat:
                                    type: number
                                  lng:
                                    type: number
                              steps:
                                type: array
                                items:
                                  type: object
                                  properties:
                                    distance:
                                      type: object
                                      properties:
                                        text:
                                          type: string
                                        value:
                                          type: integer
                                    duration:
                                      type: object
                                      properties:
                                        text:
                                          type: string
                                        value:
                                          type: integer
                                    end_location:
                                      type: object
                                      properties:
                                        lat:
                                          type: number
                                        lng:
                                          type: number
                                    html_instructions:
                                      type: string
                                    polyline:
                                      type: object
                                      properties:
                                        points:
                                          type: string
                                    start_location:
                                      type: object
                                      properties:
                                        lat:
                                          type: number
                                        lng:
                                          type: number
                                    travel_mode:
                                      type: string
                                    maneuver:
                                      type: string
                  status:
                    type: string
        '400':
          description: Invalid request
        '401':
          description: Invalid API key
        '500':
          description: Internal server error

I have added my Directions API key to the “Authentication” part of the GPT action, type “API key”, auth type “Custom” and Custom Header Name “key”

I cannot get it work. Every time it complains about “API key invalid”.

For now the only way to get it work is to directly put the API key within the OpenAI Schema:

parameters:
        - name: key
          in: query
          required: true
          schema:
            type: string
          description: xxxxxxxxx (my key)

which I don’t think is a good practise as API key should be part of “Authentication” and should not be explicit as part of the schema.

But I don’t know how to configure “Authentication” part to make it work.

Any help?

The Google APIs don’t take API keys in headers, they need to go in a query string parameter which GPTs didn’t currently support.

As such, you cannot safely call a Google API directly. You’ll need to pass it through an API gateway you can’t and add your Google API key to the query string there.

You’ll want to protect your gateway with your own authentication system.

1 Like