Automatic importing of collections with Postman API

Im trying to create a script using the Postman API to import a URL as a collection. When I use the import button through the app, it works, but doing with the script returns that it is missing a required parameter “collection” within the json.

Failed to import collection. Status code: 400
Response body: {"error":{"name":"paramMissingError","message":"Parameter is missing in the request.","details":{"param":"collection"}}}

Hi @jthom2. Welcome to the Postman Community.

AFAIK, you can only import a Postman data dump or an OpenAPI definition via the Postman API. Can you share how you’re trying to do this import using the Postman API?

Yes, the code below is what im using.

from dotenv import load_dotenv
import requests
import json
import os

load_dotenv()

api_key = os.environ.get('POSTMAN_API_KEY')

api_url = os.environ.get('API_URL')

api_response = requests.get(api_url)
if api_response.status_code == 200:
    api_definition_content = api_response.json()
else:
    print("Failed to fetch API defintion. Status code:", api_response.status_code)
    exit()

url = "https://api.getpostman.com/collections"

headers = {
    "X-Api-Key": api_key,
    "Content-Type": "application/json"
}

api_definition_json = json.dumps(api_definition_content)

response = requests.post(url, headers=headers, data=api_definition_json)

if response.status_code == 200:
    print("Collection created successfully")
else:
    print("Failed to import collection. Status code:", response.status_code)
    print("Response body:", response.text)

It seems you’re making an API call to the wrong endpoint. You can refer to the documentation for importing dumps here.

I was able to get it working with that endpoint and a few other minor modifications. Thank you!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.