Skip to content

Instantly share code, notes, and snippets.

@humphd
Created November 2, 2023 15:15
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Convert a DOI or DOI URL to JSON via CrossRef API
/**
* Gets JSON citation data from the CrossRef API for a DOI or DOI URL
* @param doi A DOI or DOI URL
*/
export async function doi2json(doiOrUrl: string) {
// Extract the DOI from the URL
const doi = doiOrUrl.replace('https://doi.org/', '');
// Fetch data from the CrossRef API
const response = await fetch(`https://api.crossref.org/works/${doi}`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment