> For the complete documentation index, see [llms.txt](https://docs.os.uk/more-than-maps/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.os.uk/more-than-maps/os-downloads-api/using-the-os-downloads-api-with-python.md).

# Using the OS Downloads API with Python

## Process Overview

You will:&#x20;

1. Set things up once (Python environment, database, schema).&#x20;
2. Run Script 1 once to create the correct database schema.&#x20;
3. Run Script 2 repeatedly (manually or on a schedule) to:&#x20;

* Check if OS has released a newer data package&#x20;
* Download it if needed&#x20;
* Load it safely into your database&#x20;

After setup, you will normally only touch Script 2.&#x20;

## Prerequisites

* A Windows machine (this guide uses Windows paths and Task Scheduler)&#x20;
* Python 3.9+ installed&#x20;
* Access to a PostgreSQL database (local or remote)&#x20;
* An OS Data Hub account with access to the Downloads API&#x20;

You will also need:&#x20;

* An API Key from OS Data Hub&#x20;
* The Package ID of the dataset you want to automate&#x20;
* CSV&#x20;
* Monthly or Annual FULL REFRESH&#x20;

## Step 1: Create a working folder

Create a new folder anywhere on your machine, for example:

```sql
C:\os_downloads_automation
```

Inside this folder you will eventually have:&#x20;

* A Python virtual environment&#x20;
* Script 1 (schema setup)&#x20;
* Script 2 (download + upload)&#x20;
* A run.bat file (for automation)&#x20;

## Step 2: Create and activate a Python virtual environment

Open Command Prompt and run:

```plsql
cd C:\os_downloads_automation 

python -m venv .venv 
```

Activate it:

```
.venv\Scripts\activate
```

You should now see the virtual environment name in your prompt.&#x20;

## Step 3: Install the required Python Packages

With the virtual environment activated, install the dependencies:&#x20;

```
pip install psycopg2-binary requests
```

The scripts also use standard Python libraries which do not need installing:&#x20;

* os&#x20;
* sys&#x20;
* zipfile&#x20;
* logging&#x20;
* datetime&#x20;
* time&#x20;

## Step 4: Enable the Downloads API in OS Data Hub and initiate your data package

1. Log in to OS Data Hub&#x20;
2. Open your API Project&#x20;
3. Add Downloads API to the project&#x20;
4. Copy your API Key – you will need this in Script 2&#x20;
5. Create data package containing the data that you would like to upload. Select CSV and chose monthly or annual FULL REFRESH.&#x20;
6. Create datapackage and note the datapackage number&#x20;

## Step 5: Script 1 - Upload the database schema (RUN ONCE!)

This script prepares your database so the data can be loaded correctly.&#x20;

#### When do I run this?&#x20;

* The first time you load a dataset&#x20;
* If OS releases a new schema and you update it&#x20;

You do not run this every day.&#x20;

#### What you need to edit&#x20;

In the first cell / config section of Script 1:&#x20;

* Database connection details&#x20;
* TARGET\_SCHEMA&#x20;

#### Important naming rule&#x20;

Script 2 expects schemas to follow this pattern:&#x20;

```
osngd_{theme_abbreviation}_{feature_type}
```

Example:&#x20;

* OS NGD Building > Building Features&#x20;

```
osngd_bld_fts
```

#### Add the schema SQL&#x20;

1. Go to the OS NGD schema repository:&#x20;

[https://github\\.com/OrdnanceSurvey/osngd\\-resources/tree/main/database\\-resources/postgresql/ddl](https://github.com/OrdnanceSurvey/osngd-resources/tree/main/database-resources/postgresql/ddl)[osngd-resources/database-resources/postgresql/ddl at main · OrdnanceSurvey/osngd-resources · GitHub](https://github.com/OrdnanceSurvey/osngd-resources/tree/main/database-resources/postgresql/ddl)&#x20;

2. Open the schema you need&#x20;
3. Copy **the entire SQL file**&#x20;
4. Paste it into the **schema upload section** of Script 1&#x20;

#### Run Script 1&#x20;

Save the file and run it once:&#x20;

```
python 1_schema_setup.py
```

If this completes successfully, you do not need to touch Script 1 again unless the schema changes.&#x20;

{% file src="/files/4BxidX2vkWpBlTmilopc" %}

## Step 6: Download and upload data

#### What Script 2 does&#x20;

* Checks OS for the latest version of the data package&#x20;
* Compares it to what is already installed&#x20;
* Downloads new data if required&#x20;
* Loads data into a staging schema&#x20;
* Safely replaces existing tables&#x20;
* Logs successes and failures&#x20;

#### What you need to edit&#x20;

At the top of Script 2, update:&#x20;

* API Key&#x20;
* Package ID&#x20;
* Download location&#x20;
* Database connection details&#x20;
* Staging schema name&#x20;

Save the file.&#x20;

#### Test Script 2 manually&#x20;

Run:&#x20;

```
python 2_download_and_upload.py
```

Confirm that:&#x20;

* Data downloads correctly&#x20;
* Tables appear in the target schema&#x20;
* A log file is created

{% file src="/files/Vh13pkYIgn4oxyTwjUZo" %}

{% file src="/files/4zT8xF4BpolM90yOJgX5" %}

## Step 7: Create a run.bat file

This allows Windows Task Scheduler to run the script correctly.&#x20;

#### Create the file&#x20;

In your main folder (C:\os\_downloads\_automation), create a file called:&#x20;

```
run.bat
```

#### Edit run.bat&#x20;

Open it in Notepad and paste:&#x20;

```
cd C:\os_downloads_automation 

.venv\Scripts\python.exe 2_download_and_upload.py 
```

Adjust the path if your folder is different.&#x20;

#### Test it&#x20;

Double‑click run.bat.&#x20;

## Step 8: Automate with Windows Task Scheduler

#### Create the task&#x20;

1. Open **Task Scheduler**
2. Select **Create Basic Task**&#x20;

#### Task details&#x20;

* **Name**: OS Downloads API Refresh&#x20;
* **Description**: Automatically checks for new OS data releases and updates the database&#x20;

#### Trigger&#x20;

* Choose how often to run&#x20;
* Monthly is typical if your data package refreshes monthly&#x20;

#### Action&#x20;

* Select **Start a program**&#x20;
* Browse to and select run.bat&#x20;

#### Finish&#x20;

Save the task.

## Common troubleshooting tips

* **Nothing downloads**: Check API key and package ID&#x20;
* **Database errors**: Check credentials and schema name&#x20;
* **Script runs but no data changes**: Data may already be the latest version&#x20;
* **Task Scheduler fails**: Re‑test run.bat manually first&#x20;


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.os.uk/more-than-maps/os-downloads-api/using-the-os-downloads-api-with-python.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
